Magento 2 – How to Add Custom Link to Top Links Section

magento2menutoplinks

I have a top. links in default.xml in my theme, I need to add the custom link above My Account, but the link is always at the bottom, how I can move to the top if sortOrder not working?

<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/[email protected]</argument>
                <argument name="logo_width" xsi:type="number">300</argument>
                <argument name="logo_alt" xsi:type="string">Auto Show Fire</argument>
            </arguments>
        </referenceBlock>
        
         <referenceContainer name="header.container" remove="true">
            <referenceContainer name="header.panel.wrapper" remove="true" />
            <referenceContainer name="header.panel" remove="true" />
            <referenceBlock name="top.search" remove="true"/>
        </referenceContainer>

        <referenceContainer name="header-wrapper">
            <referenceBlock name="navigation.sections" remove="true" />
        </referenceContainer>

        <move element="top.links" destination="custom_header"/>

        <referenceBlock name="top.links">

            <block class="Magento\Framework\View\Element\Html\Link" name="custom-top-link">
                <arguments>
                    <argument name="sortOrder" xsi:type="number">1000</argument>
                    <argument name="label" xsi:type="string" translate="true">Custom Top Link</argument>
                    <argument name="path" xsi:type="string">*/*/*</argument>
                </arguments>
            </block>

            
            <block class="Magento\Customer\Block\Account\Link" name="help-link" before="authorization-link">
                <arguments>
                    <argument name="path" xsi:type="string">contacts</argument>
                    <argument name="label" xsi:type="string">Help</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

<argument name="sortOrder" xsi:type="number">1000</argument> – don't work. Any Idea how I can move this custom link at the top so that it is always located at the top of the menu.

Best Answer

Try this,

create default.xml file in app/code/Vendorname/ModuleName/view/frontend/layout directory.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="top.links">
            <block class="Magento\Framework\View\Element\Html\Link" name="my-top-link">
                <arguments>
                    <argument name="label" xsi:type="string" translate="true">My Top Link</argument>
                    <argument name="path" xsi:type="string">*/*/*</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

top.links: To add custom link in Top links.

ss :- https://i.stack.imgur.com/zybl0.png

flush the layout cache. Thanks!

Related Topic