0

I am using magento1.7 and added page title(added from backend)for some categories and some have no page titles. i want to edit the default page title for only those who have no page titles.But i can't understand, from where did get the default meta title for categories(not added page title from backend).please help me to find the file and code

1

You can do this via observer

Config.xml

<!-- File# <your module dir>/etc/config.xml -->
<frontend>
     --- 
    <events>
        <core_block_abstract_to_html_before>
            <observers>
              <core_block_abstract_to_html_before_handler> 
                <type>singleton</type>
                <class>Namespace_Modulename_Model_Observer</class>
                <method>updatePageTitle</method>
              </core_block_abstract_to_html_before_handler>
            </observers>
      </core_block_abstract_to_html_before>
    </events>    
    ---
</frontend>

Observer.php

<?php
#File: <your module dir>/Model/Observer.php 
class Namespace_Modulename_Model_Observer {

    public function updatePageTitle($observer) {
        $block = $observer->getBlock();
        if($block instanceof Mage_Page_Block_Html_Head) {
            if(Mage::registry('current_category') && !Mage::registry('current_category')->getMetaTitle()){
                $head = $block->getLayout()->getBlock('head');
                $head->setTitle("Your Title");
                $head->setKeywords("your, keywords, anything");
                $head->setDescription("Your Description");
            }
        }
    }
}
  • what details do you want ?. If you don't have a module for it. Create a simple module and add the observer from above code . Simple Observer Module Links link – Priyank Oct 21 '16 at 5:42
0

You can change the title of the category page programmatically by observing to the event controller_action_layout_generate_blocks_after.

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>

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

public function setCategoryPageTitle(Varien_Event_Observer $observer)
{
    $category = Mage::registry('current_category');
    //ensure modification applies only for category page.
    if ($category) {
        $title = $category->getMetaTitle();
        //check if default title is not set
        if(!$title) {
            //get head block
            $head = $observer->getLayout()->getBlock('head');
            $title = 'your-custom-title';
            //set new title
            $head->setTitle($title);
        }
    }
}

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