I'm creating a simple page in my Magento 2 instance.
in the controller for that page I'm trying to set a title for the page and a meta title that should be different.
But I'm not able to do that.
My layout file for the action is almost empty:

<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">


and my controller action looks like this:

namespace [Namespace]\[Module]\Controller\[Entity];

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends Action
    protected $resultPageFactory;
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
    public function execute()
        $resultPage = $this->resultPageFactory->create();

The result is this:

But the meta title is the same as the title (Something).
Changing the line in the controller:



$resultPage->getConfig()->getTitle()->set('Something else');  

results in the title (H1 tag) and the meta title changing to something else.

How can I set a different meta title and a title (h1 tag) to a page

4 Answers 4


This is how I managed to get different Meta Title and Page Heading. I have below code In my block class:

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Framework\View\Page\Config $pageConfig,
    $data = array()
) {
    $this->_scopeConfig = $scopeConfig; 
    $this->_pageConfig = $pageConfig;   

    parent::__construct($context, $data);

 * Prepare global layout
 * @return $this
protected function _prepareLayout()

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

        $this->_pageConfig->setKeywords('Meta Keywords');

        $this->_pageConfig->setDescription('Meta Description');

    $pageMainTitle = $this->getLayout()->getBlock('page.main.title');
    if ($pageMainTitle) {
        $pageMainTitle->setPageTitle('Page Heading Title');

    return parent::_prepareLayout();

[EDIT by OP]
You can set the page title from the controller also, like this:

$resultPage = $this->resultPageFactory->create();
$pageMainTitle = $resultPage->getLayout()->getBlock('page.main.title');
if ($pageMainTitle && $pageMainTitle instanceof \Magento\Theme\Block\Html\Title) {
    $pageMainTitle->setPageTitle('Page title here');
//rest of the code here.
  • 1
    I don't know if setting the page title from a content block is the best idea, but your answer got me in the right direction. Setting the page title on the page.main.title block works in controllers also. Thanks.
    – Marius
    Jul 12, 2016 at 14:11
  • Hi, thanks for sharing this. Would you mind to share the whole code in your answer (Is it Main.php?; ..layout.xml; template.phtml) you have used for this? I somehow can not reproduce/I do not see any result in my case. That would be super-awesome!
    – eskaliert
    Jan 19, 2017 at 19:25

Just tried myself for a new custom extension, it works for me :

public function execute() {
    $resultPage = $this->resultPageFactory->create();
    // Handling page title
    $resultPage->getLayout()->getBlock('page.main.title')->setPageTitle('Page title here');
    // Handling meta_title, meta_description and meta_keywords
    $resultPage->getConfig()->setKeywords("Key Words");
    return $resultPage;

It shoud do the job !

  • where did u add this? Nov 7, 2019 at 9:38
  • In the execute() method of my Action Controller
    – rou6e
    Nov 7, 2019 at 13:10

What about using:

$resultPage->getConfig()->getMetadata()->setMetadata('title', 'Something else');

for meta title.

  • it doesn't work. ->getMetadata() returns an array so I cannot call setMetadata on the result. And removing getMetadata from the line just results in an error Object of class Magento\Framework\View\Page\Title could not be converted to string.
    – Marius
    Jul 12, 2016 at 12:59
  • by using setMetadata(), you can set custom meta property like $resultPage->getConfig()->getMetadata()->setMetadata('meta_title', 'Something else'); Jul 12, 2016 at 13:09
  • @JaiminParikh. You cannot chain the 2 calls getMetadata()->setMetadata because getMetadata returns an array and you cannot call a method from an array. I explained this in the comment above. But I tried with $resultPage->getConfig()->setMetadata('meta_title', 'Something else'); and there is no change. I think this does not work because there is no <meta name="title"... rendered in the page. the title renders as <title>...</title>
    – Marius
    Jul 12, 2016 at 13:19
  • @Marius yes i am wrong at that. but by using $resultPage->getConfig()->setMetadata('meta_title', 'Something else'); , t will create new meta tag which name is "meta_title" and it will be no useful. Jul 12, 2016 at 13:32

For the meta title you have to use the



 $pageConfig->setMetaTitle('Meta title value')

where $pageConfig is object of \Magento\Framework\View\Page\Config.


if you use $pageConfig->getTitle()->set(" My Title") then vale set for both meta title and <title> field

<meta name="title" content="My Title"/>

<title>My Title</title>

If you use

$pageConfig->getTitle()->set(" My Title");
$pageConfig->setMetaTitle('Meta title value');

The expected result will be:

<meta name="title" content="My Title"/>

<title>Meta title value</title>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.