I am trying to get the current pages meta data in title.phtml on Magento 2 so that I can have a sub title and slight description on the page.

But I cant find anything for how to retrieve this information, only how to set it.

I have tried things like:

$this->pageConfig->getTitle();

and

$this->getLayout()->getBlock('head')->getTitle();

and

$resultPage->getConfig()->getTitle()

But all without success. I know this is all quite new at the moment but can anyone tell me how to do this?

A way to get the meta data could be:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_page = $objectManager->get('Magento\Cms\Model\Page');

$_meta_title = strip_tags($_page->getMetaTitle());
$_meta_description = strip_tags($_page->getMetaDescription());
$_meta_keywords = strip_tags($_page->getMetaKeywords());

However this does not take into account the default settings under Content > Design > Configuration > HTML Head

This only works for cms pages, i guess.

Finally i used this in my case:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_page_config = $objectManager->get('Magento\Framework\View\Page\Config');

$_meta_title = strip_tags($_page_config->getTitle()->getShort());
$_meta_description = strip_tags($_page_config->getDescription());
$_meta_keywords = strip_tags($_page_config->getKeywords());

Have look in the Magento\Framework\View\Page\Config i think there is literally a getMetadata method.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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