Setting different title and meta title for a page
Question
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
Solution
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.
OTHER TIPS
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 !
What about using:
$resultPage->getConfig()->getMetadata()->setMetadata('title', 'Something else');
for meta title.