Get the meta description and page title on any pag

2019-03-03 18:59发布

问题:

How can I get the meta description and page title of any page in Magento (product page, category page, CMS page and any other page). This is for Magento 1.9.

I have tried something along the lines of:

if( Mage::registry('current_product') ){  // product page
    $product = Mage::registry('current_product');
    $title = $product->getMetaTitle();
    $descr = $product->getDescription();
}elseif( Mage::registry('current_category') ){  // category page
    $category = Mage::registry('current_category');
    $title = $category->getTitle();
    $descr = $category->getDescription();
}else{  // CMS / any other page
    $title = $this->getTitle();
    $descr = $this->getDescription();
}

But this isnt working in every case. Can anyone help?

回答1:

To get the page title on any page, use:

$title = $this->getLayout()->getBlock('head')->getTitle()

To get the meta description on any page, use:

$descr = $this->getLayout()->getBlock('head')->getDescription()