7

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

12

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.
  • 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 '16 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 '17 at 19:25
0

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

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 !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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