1

I'm getting issues with website meta info. If I will set the default meta info then it will display the same info tag for all the pages.

But when I'm trying to set the meta info for a particular page then it will not display the Meta title & description.

I have set the Meta description & title for one category. In this case, the Meta description & title tag showing for the same category page.

Solution I applied to display both the tags for category pages:

I have created one observer & set the description for the current category. But when I tried to set the title then I got the below error.

Recoverable Error: Object of class Magento\Framework\View\Page\Title could not be converted to string in /var/www/html/htdocs/vendor/magento/framework/View/Page/Config/Renderer.php on line 125

I used the below code to set the meta title & description.

$this->_pageConfig->setMetadata('title', 'some meta title');

$this->_pageConfig->setDescription($desc);

$this->_pageConfig->setKeywords($keyWrds);

Thanks for any help.

2

2 Answers 2

0

Use this code to set Metatitle,

$this->_pageConfig->getTitle()->set(__('Your Title'));

5
  • Hi @aravind, I have checked the same. It's also not working. It will not add the meta tag. Jun 4, 2019 at 12:31
  • hello @DeepakKumar could you see the description and keywords set?
    – aravind
    Jun 4, 2019 at 12:33
  • The keywords showing by default. But the description also was not showing so I have added the meta description through observer. So now the description is showing but the title not. Jun 4, 2019 at 12:44
  • could you show me the updated code of setting the title.
    – aravind
    Jun 4, 2019 at 12:52
  • I have set the title by using the above shared functions by me & the function shared by you. But nothing working for the same. Jun 4, 2019 at 13:03
0

Refer block and its method,

\Magento\Catalog\Block\Category\View::_prepareLayout()

In this way you can set the meta title and Description,

$category = $this->getCurrentCategory();
if ($category) {
    $title = $category->getMetaTitle();
    if ($title) {
        $this->pageConfig->getTitle()->set($title);
    }
    $description = $category->getMetaDescription();
    if ($description) {
        $this->pageConfig->setDescription($description);
    }
    $keywords = $category->getMetaKeywords();
    if ($keywords) {
        $this->pageConfig->setKeywords($keywords);
    }
    if ($this->_categoryHelper->canUseCanonicalTag()) {
        $this->pageConfig->addRemotePageAsset(
            $category->getUrl(),
            'canonical',
            ['attributes' => ['rel' => 'canonical']]
        );
    } 

}
1
  • Hi @oscprofessionals it will update the meta title into the page title. But my concern is with the meta title tag that not showing to the category & product page. Jun 13, 2019 at 13:18

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.