1

i want to add itemprop in my title and description on category pages only , i can not find any option for same please guide me for it

<title itemprop="name">title here </title>
<meta itemprop="description" name="description" content="Description here "/>
0
+50

You can find the methods that generate the HTML tags in the Magento_Framework::view/page/config/renderer.php file.

A simple plugin to extend them that either uses regex or reimplement the logic of them to add the itemprop tags.

The relevant method for the title is renderTitle and the meta tag is within the protected method getMetadataTemplate ¹


Example Plugin, this is untested but a a rough guide

<?php

namespace Vendor\Module\Plugins;

use Magento\Framework\View\Page\Config\Renderer;

class RendererPlugin
{
    public function afterRenderTitle(Renderer $subject, $result)
    {
        return preg_replace('/^<title>/', '<title itemprop="title">', $result);
    }

    public function afterGetMetadataTemplate(Renderer $subject, $result)
    {  
        return preg_replace(
            '/(<meta(?!.*itemprop="description".*).*name="description")/im',
            '$1 itemprop="description"',
             $result
        );
    }
}

https://github.com/magento/magento2/blob/2.4-develop/lib/internal/Magento/Framework/View/Page/Config/Renderer.php#L147

¹ https://github.com/magento/magento2/blob/2.4-develop/lib/internal/Magento/Framework/View/Page/Config/Renderer.php#L205

  • where should i place above code – user1799722 Dec 11 '19 at 4:56
  • where should i place above code, i tired placing it inside "Vendor/Module/Plugins/Renderer.php" but is is not working – user1799722 Dec 11 '19 at 5:05
0

Reference Link

In order to have the Product schema.org markup somewhere other than the default location of the body tag, you need to first remove the default Magento output.

It is possible to "unset" the schema.org body attributes added by default by adding the following layout update for the default handle (i.e. in default.xml in your theme or a module):

 <body>
    <attribute name="itemtype" value="" />
    <attribute name="itemscope" value="" />
 </body>

Now you're able to place the itemtype and itemscope markup elsewhere as needed. Do this either in templates or read on for how to do it via adding attributes to containers in layout XML, as the title of your question seems to request.

Adding arbitrary attributes to existing or new containers is not possible by default. However, by extending Magento 2 it is possible to do it without altering _renderContainer (which is a really really bad idea and not necessary.)

I actually needed to do something similar to you and the OP of your linked question, and have posted a solution to that specific problem there: Magento 2 container custom attribute. Go check that out, it should get you the solution to the other part of your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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