Our brands

How to add top link?

With venus themes you can add custom top links by 2 ways:

1. Add custom top links in ves theme settings in backend:

Ves Themesettings module support very good this feature. You can go to backend > Ves Theme settings > Header > Top Links, then add custom link on top-link you want to show

– With Link option: you can input the link route (ex: contact, the link on frontend will been generated to http://yourdomain.com/contact). Or you can input direct link at here.

The code to call custom links we defined in the file : app\design\frontend\Venustheme\theme_name\Ves_Themesettings\templates\html\link.html

 

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile
?>

<?php
$ves = $this->_ves;
$vesData = $this->_vesData;
$html = '';
$id = $block->getIdModifier() ? '-' . $block->getIdModifier() : time();
?>

<div class="header-links" id="header-links<?php echo $id?>">
    <div class="actions dropdown options header-links-options">
        <div class="action toggle switcher-trigger" id="header-links-trigger<?php echo $id?>">
            <strong><?php echo __('My Account') ?> </strong>
        </div>
        <ul class="dropdown header-links-dropdown" data-mage-init='{"dropdownDialog":{
        "appendTo":"#header-links<?php echo $id?> > .options",
        "triggerTarget":"#header-links-trigger<?php echo $id?>",
        "closeOnMouseLeave": false,
        "triggerClass":"active",
        "parentClass":"active",
        "buttons":null}}'>
        <?php
        foreach ($this->getLinks() as $link) {
            $html .= $this->renderLink($link);
        }
        echo $html;
        ?>
        <?php
        //Start call custom top links in ves theme settings
        $custom_links = unserialize($ves->getHeaderCfg('toplinks_settings/custom_links'));
        unset($custom_links['__empty']);
        foreach ($custom_links as $_link) { ?>
        <li class="<?php echo $_link['classes'] ?>" >
            <a href="<?php echo $vesData->filter($_link['link']) ?>" target="<?php echo $_link['target'] ?>"><?php echo $_link['title'] ?></a>
        </li>
        <?php } ?>
    </ul>
</div>
</div>

 

2. You can add custom top links by layout xml:

For example: we create a link to a custom cms page “Contact Us” with route “contact-us”.

– You need to create a CMS page for example you create an “Contact Us” page with identifier as “contact-us”
The ink access to contact page on frontend: http://domain.com/contact-us

– To add magento 2 top links, you need to add following code in default.xml of your theme
Add code in body tag of a file *.xml. For example:

app/design/frontend/Magento/YOUR_THEME/Magento_Theme/layout/default.xml

<referenceBlock name="header.links">
	<block class="Magento\Framework\View\Element\Html\Link" name="contact-link">
		<arguments>
			<argument name="label" xsi:type="string" translate="true">Contact Us</argument>
			<argument name="path" xsi:type="string" translate="true">contact-us</argument>
		</arguments>
	</block>
</referenceBlock>

The previous code will be redirected to “Magento\Framework\View\Element\Html\Link” class and tranfers label = Contact Us and path = contact-us (http://domain.com/contact-us)
After that, Check the PHP Magento\Framework\View\Element\Html\Link.php file, there is one link shown on frontend

 

protected function _toHtml()
{
    if (false != $this->getTemplate()) {
        return parent::_toHtml();
    }
	return '<li><a ' . $this->getLinkAttributes() . ' >' . $this->escapeHtml($this->getLabel()) . '</a></li>';
}

More information, please view the link: http://magento.stackexchange.com/questions/92333/in-magento-2-0-how-can-i-add-new-top-links-and-remove-existing-one-using-xml

Share this Post!

Panda Thuan
Panda Thuan
Creativity is falling in love with the world

0 Comment

Do NOT follow this link or you will be banned from the site!