0

I want to add of tags for facebook social media in Magento 2. So I do not know by where I can add this from code. Please help me to figure out this.

  • u can check in file vendor\magento\module-catalog\view\frontend\templates\product\view\opengraph/general.phtml it is already set. Is it appearing on the website browser source? – Amrit Pal Singh Oct 23 at 10:20
  • Do you want to add og tags for all pages ? – Kishan Savaliya Oct 23 at 10:27
  • @AmritPalSingh: But this is only working for catalog product detail pages. I want to add on homepage and other cms pages – Shashank Sharma Oct 23 at 10:27
  • @ShashankSharma, Please review my answer. – Kishan Savaliya Oct 23 at 10:31
1

You can add one block in cms_index_index.xml file in your custom theme and you can add your OG tags there.

app/design/frontend/Vendor/Theme/Magento_Theme/layout/cms_index_index.xml

Content for this file is ..

<?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">
    <head>
        <attribute name="prefix" value="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# product: http://ogp.me/ns/product#" />
    </head>
    <body>
        <referenceBlock name="head.additional">
            <block class="Magento\Framework\View\Element\Template" name="opengraph.general" template="Magento_Theme::html/header/meta.phtml"/>
        </referenceBlock>
    </body>
</page>

You can create template here..

app/design/frontend/Vendor/Theme/Magento_Theme/templates/html/header/meta.phtml

Content for this file is..

<?php
    $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $request = $_objectManager->get('Magento\Framework\App\Action\Context')->getRequest();
?>
<?php if($request->getFullActionName() == 'cms_index_index') : ?>
    <meta property="og:type" content="test" />
    <meta property="og:title" content="test" />
    <meta property="og:description" content="test" />
    <meta property="og:url" content="test" />
    <meta property="og:image" content="test" />
<?php endif; ?>

Hope this will help you!

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.