magentoproductmeta-tags

Custom Magento Meta Title


I'm using magento 1.9. On product pages the meta title is like

"PRODUCT NAME CATEGORY SUBCATEGORY"

I would like to show only "PRODUCTNAME - Mysite.com"

How can I make this fix only on product pages?

Thanks.


Solution

  • Magento sets title tag in two different places. First in Mage_Catalog_Block_Breadcrumbs inside _prepareLayout() method

        $title = array();
        $path  = Mage::helper('catalog')->getBreadcrumbPath();
    
        foreach ($path as $name => $breadcrumb) {
            $breadcrumbsBlock->addCrumb($name, $breadcrumb);
            $title[] = $breadcrumb['label'];
        }
    
        if ($headBlock = $this->getLayout()->getBlock('head')) {
            $headBlock->setTitle(join($this->getTitleSeparator(), array_reverse($title)));
        }
    

    Content of $path variable will vary depending on how you got to the product page. For example:

    Direct link to product:

    Navigate to product using category menu:

    And then in Mage_Catalog_Block_Product_View inside _prepareLayout() method

        $product = $this->getProduct();
        $title = $product->getMetaTitle();
        if ($title) {
            $headBlock->setTitle($title);
        }
    

    Here it simply checks if meta title product attribute is set and overrides previously set title if so. So, you have two options:

    1. Set meta title on every product
    2. Rewrite _prepareLayout() method in Mage_Catalog_Block_Breadcrumbs class

      if ($headBlock = $this->getLayout()->getBlock('head')) {
          if ($product = Mage::registry('current_product')) {
              $storeName = Mage::getStoreConfig('general/store_information/name');
              $pageTitle = $storeName ? $product->getName() . ' - ' . $storeName : $product->getName();
              $headBlock->setTitle($pageTitle);
          } else {
              $headBlock->setTitle(join($this->getTitleSeparator(), array_reverse($title)));
          }
      }