0

i been trying to add some links as shown in the picture...my intention is to add a phtml template block so i can easily modify in the future, how can i do this?

i am using magento 2.4.4, i have tried many things, one of them is using a simple block class that works in the container referenceContainer name="main...but does not work on referenceBlock name="top.links

Here is the block

block class="Magento\Framework\View\Element\Template" name="mylinks" template="Magento_Theme::html/mylinks.phtml

my goal

1 Answer 1

0

In your custom module you can do this using the following code:

app\code\vendor\ModuleName\view\frontend\layout

In here add a file called default.xml and place the following code:

<referenceBlock name="header.links">
<block class="Vendor\ModuleName\Block\CustomLink" name="custom-header-link">
    <arguments>
        <argument name="label" xsi:type="string" translate="true">Custom Header Link</argument>
    </arguments>
</block>

in app\code\vendor\ModuleName\Block place the following file:

<?php
namespace Vendor\ModuleName\Block;

class CustomLink extends \Magento\Framework\View\Element\Html\Link
{
    public function getHref()
    {
        //place your link here
        return 'https://www.google.com/';
    }

    public function getTarget()
    {
        return '_blank';
    }
}

You will need to compile your code and clean cache.

If this solved your issue please upvote and mark as answered.

1
  • Thanks, that solved my question with a little tweak... Jun 1, 2022 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.