Question

How do I create a default category meta title for all the category pages?

I would like it to be something like this:

<title>{{category name}} - blah blah blah blah</title>

I want it to be the default only for category pages.

Is this possible?

Was it helpful?

Solution

Add the following code in your head.phtml

if (Mage::registry(current_category)) {
    echo '<title> Categoryname- keyword1 keyword2 keyword3"';
}

EDIT

As far as I know Mage::registry('current_category') can be used to check whether we are in a category. See Alanstorm's Answer. The important parts of that answer is adding below for reference

Current versions of Magento register certain global variables (not PHP globals, but things global to the Magento system) on certain pages.

Calling the following

   $category = Mage::registry('current_category');         
   $product  = Mage::registry('current_product');
   $product  = Mage::registry('product');

will either return null if the objects haven't been set (i.e. you're on a page without a category or a product), or return category and product objects.

If a product object is returned you're on a product page.

If no product object is returned but a category object is, you're on a category page.

then you can print what you want.

OTHER TIPS

The most cleaner way to perform this is by observing to the event controller_action_layout_generate_blocks_after. So the important parts of your module (Namespace_Module) would be

File : app\code\local\Namespace\Module\etc\config.xml

<config>
    <frontend>
        <events>
            <controller_action_layout_generate_blocks_after>
                <observers>
                    <change_category_page_title>
                        <class>namespace_module/observer</class>
                        <method>setCategoryPageTitle</method>
                    </change_category_page_title>
                </observers>
            </controller_action_layout_generate_blocks_after>
        </events>
    </frontend>
</config>

Here we are registering an observer to the event controller_action_layout_generate_blocks_after. This particular event will get triggered, after every blocks is generated from the layout. So at this particular time, we can access every blocks in the Layout. The block that we are interested here is head block (ie Mage_Page_Block_Html_Head). This block is holds the title element.

File : app\code\local\Namespace\Module\Model\Observer.php

<?php
class Namespace_Module_Model_Observer
{

    const CATEGORY_TITLE_CONSTANT = 'blah blah blah blah';

    public function setCategoryPageTitle(Varien_Event_Observer $observer)
    {
        //ensure modification applies only for category page.
        if (Mage::registry('current_category')) {
            //get head block
            $head = $observer->getLayout()->getBlock('head');
            $title = $this->_prepareCategoryTitle();
            //set new title
            $head->setTitle($title);
        }
    }

    protected function _prepareCategoryTitle()
    {
        return Mage::registry('current_category')->getName()
            . '-'
            . self::CATEGORY_TITLE_CONSTANT;
    }
}

This is our observer class. The method that will alter category title is setCategoryPageTitle(). What this function does is simple. It ensures we are in a category page. Then it will grab the head block and set a new title. To set a new title, we are using a protected function _prepareCategoryTitle(). I defined this function according to this question's need. Currently it will return category name + blah blah blah. So you can alter this method and change the title according to your needs.

Note: Here NO CORE EDITS, NO ALTERNATION IN DEFAULT TEMPLATES. That is why this is the cleaner method.

Basically, title of pages from meta title field of category.

For your requirment,you need to rewrite class Mage_Catalog_Block_Category_View on there you need check current title using check Head block getTitle() value and reset it value to default when it does have any value.

Rewrite class:

<?php
class Dev_AmitCatalog_Block_Catalog_Category_View extends Mage_Catalog_Block_Category_View
{
   protected function _prepareLayout()
    {
        parent::_prepareLayout();

        $this->getLayout()->createBlock('catalog/breadcrumbs');

        if ($headBlock = $this->getLayout()->getBlock('head')) {
            $category = $this->getCurrentCategory();
    if (!$headBlock->getTitle()) {
                $headBlock->setTitle($category->getName().' - blah blah blah blah');
            }
    }
}

config.xml:

  <global>
    ....
    <blocks>
      <amitcatalog>
        <class>Dev_AmitCatalog_Block</class>
      </amitcatalog>
            <catalog>
                <rewrite>
                    <category_view>Dev_AmitCatalog_Block_Catalog_Category_View</category_view>
                </rewrite>
            </catalog>
    </blocks>
.....
  </global>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top