{"id":7306,"date":"2019-09-26T19:05:50","date_gmt":"2019-09-26T13:35:50","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7306"},"modified":"2019-10-04T11:20:10","modified_gmt":"2019-10-04T05:50:10","slug":"magento-2-add-top-link","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-add-top-link\/","title":{"rendered":"Magento 2 &#8211; How to Add Top Link"},"content":{"rendered":"<p>Any website&#8217;s header is the perfect place to display the important links. Top Links block in Magento 2 is used to add links related to customer account like Login, Logout, My Account, My Wishlist etc.<\/p>\n<p>So, in this post I am going to explain how to add top link in magento 2.<\/p>\n<p>First, Add or Modify below content default.xml file<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n\r\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\"&gt;\r\n    &lt;body&gt;\r\n        &lt;referenceBlock name=\"header.links\"&gt;\r\n            &lt;block class=\"Ht\\Mymodule\\Block\\Toplink\" name=\"add-new-top-link\"&gt;\r\n                &lt;arguments&gt;\r\n                    &lt;argument name=\"label\" xsi:type=\"string\" translate=\"true\"&gt;Top Link&lt;\/argument&gt;\r\n                    &lt;argument name=\"path\" xsi:type=\"string\" translate=\"true\"&gt;top-link&lt;\/argument&gt;\r\n                &lt;\/arguments&gt;\r\n            &lt;\/block&gt;\r\n        &lt;\/referenceBlock&gt;\r\n    &lt;\/body&gt;\r\n&lt;\/page&gt;<\/pre>\n<p>Now add the Block file Toplink.php for the custom Link.<\/p>\n<p>Path: app\/code\/Ht\/Mymodule\/Block\/Toplink.php<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\r\nnamespace Ht\\Mymodule\\Block;\r\n\r\nclass Toplink extends \\Magento\\Framework\\View\\Element\\Html\\Link {\r\n\r\n\tprotected function _toHtml() {\r\n        if (false != $this-&gt;getTemplate()) {\r\n            return parent::_toHtml();\r\n        }\r\n        return '&lt;li class=\"custom-top-link\"&gt;&lt;a ' . $this-&gt;getLinkAttributes() . ' &gt;' . $this-&gt;escapeHtml($this-&gt;getLabel()) . '&lt;\/a&gt;&lt;\/li&gt;';\r\n    }\r\n}<\/pre>\n<p>Thats it. Now, Clear the cache the check for the change.<\/p>\n<p><strong>If you do not want to use the custom Block then you can use Magento default Block. In this case you don&#8217;t need to add the Block file.<\/strong><\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n\r\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\"&gt;\r\n    &lt;body&gt;\r\n        &lt;referenceBlock name=\"header.links\"&gt;\r\n            &lt;block class=\"Magento\\Framework\\View\\Element\\Html\\Link\" name=\"add-new-top-link\"&gt;\r\n                &lt;arguments&gt;\r\n                    &lt;argument name=\"label\" xsi:type=\"string\" translate=\"true\"&gt;Top Link&lt;\/argument&gt;\r\n                    &lt;argument name=\"path\" xsi:type=\"string\" translate=\"true\"&gt;top-link&lt;\/argument&gt;\r\n                &lt;\/arguments&gt;\r\n            &lt;\/block&gt;\r\n        &lt;\/referenceBlock&gt;\r\n    &lt;\/body&gt;\r\n&lt;\/page&gt;<\/pre>\n<p><strong>Now if you want to get or show link from your template file then you can do as below.<\/strong><\/p>\n<p>Add or Modify below content default.xml file<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n\r\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\"&gt;\r\n    &lt;body&gt;\r\n        &lt;referenceBlock name=\"header.links\"&gt;\r\n            &lt;block class=\"Magento\\Framework\\View\\Element\\Html\\Link\" name=\"add-new-top-link\" template=\"Ht_Mymodule::toplink.phtml\" \/&gt;\r\n        &lt;\/referenceBlock&gt;\r\n    &lt;\/body&gt;\r\n&lt;\/page&gt;<\/pre>\n<p>Now add new template and add below code in that.<\/p>\n<pre class=\"lang:default decode:true \">&lt;li class=\"custom-top-link\"&gt;\r\n    &lt;a href=\"#\"&gt;Custom Top Link&lt;\/a&gt;\r\n&lt;\/li&gt;<\/pre>\n<p>Thats it. Enjoy Magento 2!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Any website&#8217;s header is the perfect place to display the important links. Top Links block in Magento 2 is used to add links related to customer account like Login, Logout, My Account, My Wishlist etc. So, in this post I am going to explain how to add top link in magento 2. First, Add or&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-add-top-link\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7371,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[9,10,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7306"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=7306"}],"version-history":[{"count":2,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7306\/revisions"}],"predecessor-version":[{"id":7311,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7306\/revisions\/7311"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7371"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}