Not able to add any additional attribute to Magento 2 Custom Theme's Logo Element
-
06-10-2020 - |
Question
I have successfully created a custom theme as a child of Luma theme in my Magento CE 2.0.12 installation.
But if I add below code in my layout/theme.xml
file, I am not able to add a title
attribute to img
tag of Logo:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="logo">
<arguments>
<argument name="logo_file" xsi:type="string">images/mytheme_logo.svg</argument>
<argument name="logo_alt" xsi:type="string">Custom Alt</argument>
<argument name="logo_title" xsi:type="string">Custom's Logo</argument>
<argument name="logo_img_width" xsi:type="number">200</argument>
<argument name="logo_img_height" xsi:type="number">200</argument>
</arguments>
</referenceBlock>
</body>
</page>
Please suggest how to add additional attribute to img
tag of Logo.
Solution
You need to override template of logo.phtml into your custom theme.
Follow below steps :
- Assuming you have already created custom theme and its working fine and activated !! (Location is : app/design/frontend/VendorName/themename)
- Create one folder in the your custom theme directory name Magento_Theme
- Now create logo.phtml file in "Magento_theme/templates/html/header/logo.phtml" on this location.
- Copy your default logo.phtml file from theme and paste its content into your custom logo.phtml file. Add title attribute to that image tag.
logo.phtml code should be like below :
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
/**
* @var \Magento\Theme\Block\Html\Header\Logo $block
*/
?>
<?php $storeName = $block->getThemeName() ? $block->getThemeName() : $block->getLogoAlt();?>
<span data-action="toggle-nav" class="action nav-toggle"><span><?php /* @escapeNotVerified */ echo __('Toggle Nav') ?></span></span>
<?php if ($block->isHomePage()):?>
<strong class="logo">
<?php else: ?>
<a class="logo" href="<?php echo $block->getUrl(''); ?>" title="<?php /* @escapeNotVerified */ echo $storeName ?>">
<?php endif ?>
<img src="<?php /* @escapeNotVerified */ echo $block->getLogoSrc() ?>"
alt="<?php /* @escapeNotVerified */ echo $block->getLogoAlt() ?>"
<?php echo $block->getLogoWidth() ? 'width="' . $block->getLogoWidth() . '"' : '' ?>
<?php echo $block->getLogoHeight() ? 'height="' . $block->getLogoHeight() . '"' : '' ?>
title="TESTESTESTEST" />
<?php if ($block->isHomePage()):?>
</strong>
<?php else:?>
</a>
<?php endif?>
Here i have added title="TESTESTESTEST" it shows on the logo image. Clear the cache and check
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange