Site icon Qaisar Satti's Blogs

Set meta tag Magento 2

Magento 2 set meta tag

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.

Exit mobile version