:::: MENU ::::

Set meta tag Magento 2

Magento 2 set meta tag

This tutotrial is about how to set Meta tag in magento 2. Magento is a very useful ecommerce platform.Since every Magento project requires a certain level of modification. This may involve changing an existing functionality or adding a new one. In this tutorial we will learn about how to set Meta tag in magento 2.This includes Meta title,Meta Keyword and Meta description. And in this tutorial we will learn how to do it programmatically in Magento 2.

Lets come to our topic and see an example and code.Below are the two methods. Making changes in the block and controller will help to getthe desired results.
 

Block


$this->pageConfig->getTitle()->set(__('First Hello World Module'));
//setting the page title
$this->pageConfig->setDescription('test description'); // set meta description
$this->pageConfig->setKeywords('test keywords'); // set meta keyword

 

Controller

 

namespace QaisarSatti\HelloWorld\Controller\Index;

use Magento\Framework\View\Result\PageFactory;

class Index extends Action {

protected $_resultPage;

public function __construct(
PageFactory $resultPage) {
$this->_resultPage = $resultPage;
}
public function execute() {
$result= $this->_resultPage->create();
$result->getConfig()->getTitle()->set("Meta Title"); //setting the page
$result->getConfig()->setDescription("Description"); // set meta description
$result->getConfig()->setKeywords("Key Words"); // set meta keyword

return $result;
}

I hope this tutorial is easy to understand and serve its purpose.
For any suggestions & question, please feel free to drop a comment.

Author: Qaisar Satti
Category: Magento 2
Last Modified: September 30, 2018


5 Comments

  • Reply Johny Jackson |

    Hello mates, good article and fastidious urging commented here, I am truly enjoying by these.

  • Reply Johny Jackson |

    I do not even know how I ended up here, but I thought this post was great.

    I don’t know who you are but certainly you’re going to a famous blogger
    if you are not already ๐Ÿ˜‰ Cheers!

  • Reply thila |

    I want to add meta datas dynamically in product category page in programmatically in magento 2….Am new in magento thanks in advance

  • Reply Raja Usman |

    This post is great, I was stuck but after found this article i solved my problem.

So, what do you think ?