Set meta tag Magento 2
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
//setting the page title
$this->pageConfig->setDescription('test description'); // set meta description
$this->pageConfig->setKeywords('test keywords'); // set meta keyword
Controller
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.
5 thoughts on “Set meta tag Magento 2”
Leave a Reply
You must be logged in to post a comment.
Hello mates, good article and fastidious urging commented here, I am truly enjoying by these.
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!
I am just starting to learn about all of this. Thanks!
I want to add meta datas dynamically in product category page in programmatically in magento 2….Am new in magento thanks in advance
This post is great, I was stuck but after found this article i solved my problem.