Thursday 22 January 2015

Set / Change Meta title and description in Magento 2


SET PAGE META VIA BLOCK


You need to place this code inside your _prepareLayout() function in your Block file. Below is the complete function.

public function _prepareLayout()
{

   //set page Meta's
   $this->pageConfig->setKeywords('Hello Metakeyword');
   $this->pageConfig->setDescription('Hello Metadescription');

   return parent::_prepareLayout();

} 

SET PAGE META VIA LAYOUT XML

Open your xml file inside your layout folder and place this code above the body tag.

<head>
    <meta name="description" content="XML Hello metadesc"/>
    <meta name="keywords" content="XML Hello keywords"/>

</head>

NOTE:  When you try to add title via both the methods first preference will be given to the block method(php file).

No comments:

Post a Comment