9

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">
    <body>
    </body>

</page>

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
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }
    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set('Something');
    }
}

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');

to

$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

15

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->addBodyClass('advance-sitemap');

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

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

    if($this->getMetaDescription())         
        $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.
2
  • 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
    Commented 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
    Commented Jan 19, 2017 at 19:25
2

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()->getTitle()->set("Title");
    $resultPage->getConfig()->setDescription("Description");
    $resultPage->getConfig()->setKeywords("Key Words");
    return $resultPage;
}

It shoud do the job !

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

What about using:

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

for meta title.

4
  • 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
    Commented Jul 12, 2016 at 12:59
  • by using setMetadata(), you can set custom meta property like $resultPage->getConfig()->getMetadata()->setMetadata('meta_title', 'Something else'); Commented 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
    Commented 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. Commented Jul 12, 2016 at 13:32
0

For the meta title you have to use the

\Magento\Framework\View\Page\Config::setMetaTitle($title)

Mean

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

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

  $resultPage->getConfig()->setMetaTitle($title);

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.