Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Add Top Link in Magento 2

Vinh Jacker | 12-18-2024

Add Top Link

Adding top link in Magento 2 means showing some helpful links on the top of your pages. We will discover how to do through in this tutorial. Here is the CMS block, including some personal information of the customers as My Account, My Wishlist, Login or anything you need. Click on the top link and redirect the users to a CMS page where contains more particular information. Creating the top link may also push their wishes to encourage the clients to buy more and more.

For example, on the top menu, “My Wishlist” link will save and lead them directly to the list of the product they are interested in before without checkout.

If you are searching how to add the top link on your Magento 2 store, this topic will help you complete all with many top links depending on your demands.

Step 1: Add/ modify default.xml file

Find the path app/code/Mageplaza/HelloWorld/view/frontend/layout, then add/ modify default.xml file by:

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
     <referenceBlock name="header.links">
         <block class="Mageplaza\HelloWorld\Block\Link" name="add-new-header-link">
             <arguments>
     <argument name="label" xsi:type="string" translate="true">New Link</argument>
     <argument name="path" xsi:type="string" translate="true">new-link</argument>
     </arguments>
         </block>
     </referenceBlock>
</body>

Step 2: Add Link.php file

Continue with the path app/code/Mageplaza/HelloWorld/Block and you will add the link.php file:

<?php
 
namespace Mageplaza\HelloWorld\Block;
 
class Link extends \Magento\Framework\View\Element\Html\Link
{
/**
* Render block HTML.
*
* @return string
*/
protected function _toHtml()
    {
     if (false != $this->getTemplate()) {
     return parent::_toHtml();
     }
     return '<li><a ' . $this->getLinkAttributes() . ' >' . $this->escapeHtml($this->getLabel()) . '</a></li>';
    }
}

Clear cache as the above and feel comfortable when you need to add the top link as the top menu for the customer’s experience on your Magento 2 store. Follow and enjoy it!

Related Post

Conclusion

You can put important links at the top of the website to make customers stay longer and learn more about your website. It is a great way to increase engagement and conversion for your store. I hope that this tutorial successfully tell you how to add top links in a Magento 2 store. If you have any questions, please comment below. I will get back to you as soon as possible.

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services