0

How to set different meta title for the same page. I am getting meta title data based on the page url (?id=1)

1 Answer 1

0

You can do this with plugin

app/code/Acme/StackExchange/etc/frontend/di.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Cms\Block\Page">
        <plugin name="Acme_StackExchange_Plugin_Cms_Block_PagePlugin"
                type="Acme\StackExchange\Plugin\Cms\Block\PagePlugin" sortOrder="10"/>
    </type>
</config>

app/code/Acme/StackExchange/Plugin/Cms/Block/PagePlugin.php

<?php
declare(strict_types=1);

namespace Acme\StackExchange\Plugin\Cms\Block;

use Magento\Cms\Block\Page as PageBlock;
use Magento\Framework\View\LayoutInterface;
use Magento\Framework\View\Page\Config as PageConfig;

class PagePlugin
{
    protected PageConfig $pageConfig;

    public function __construct(PageConfig $pageConfig)
    {
        $this->pageConfig = $pageConfig;
    }

    public function afterSetLayout(PageBlock $subject, PageBlock $result, LayoutInterface $layout): PageBlock
    {
        // objects
        $request = $subject->getRequest();
        $page    = $subject->getPage();

        // example how to update
        $this->pageConfig->getTitle()->set('ACME ' . $page->getTitle());
        $this->pageConfig->setKeywords('ACME ' . $page->getMetaKeywords());
        $this->pageConfig->setDescription('ACME ' . $page->getMetaDescription());

        return $result;
    }
}

Your Answer

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

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