3

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?

2

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.

6
  • seems to be true also on product pages. Jul 6 '15 at 5:53
  • Let me check and get back
    – Sundar
    Jul 6 '15 at 5:58
  • nope current_category register only works in category page. Jul 6 '15 at 6:04
  • weird it does not for me. however i found a way to get over it ( Mage::registry('current_product') ) Jul 6 '15 at 6:06
  • 1
    Sorry. Yar. I am not aware that.. Let me edit ma answer. :)
    – Sundar
    Jul 6 '15 at 6:38
2

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.

0

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>
1
  • Where should the rewrite class be located? as I have put it in /Block/Catalog/Category/View.php in my extension folder but no joy
    – Goose84
    Feb 11 '16 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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