Call our sales line
08000 484 679
Home > Magento SEO > Show a Custom H1 Heading on Magento Category Pages

Show a Custom H1 Heading on Magento Category Pages

Posted on: 17th Dec 2012 By: Adam Moss No Comments

It may or may not have come to your attention that the category name that you set for your categories is used as your first heading on that category page and is also used in your main navigation. This is fine, but what happens when you want them to be different? If you want a more descriptive heading you’re going to potentially ruin your menu navigation with a long category title.

I have developed this small extension to conquer that issue. It simply creates a new category attribute called ‘Category Heading’, and if a value is given to this attribute, the category will use that as it’s H1 heading instead of the Category Name.

I have attached the module as a download at the bottom of this post. For those interested, here are the scripts I’m using:

Creare/Seoheading/Model/Category.php

I am simply overwriting the getName() method for a Category. The danger is that if you use this anywhere else it will overwrite it there too. I have put a Mage::registry check around it so that it will only affect getName() when used on the category page. If you intend to use getName() more than once on the category page, you could simply create a new getName() method, return it’s parent and just call it out as something else on your category page.

<?php

class Creare_Seoheading_Model_Category extends Mage_Catalog_Model_Category
{

	public function getName()
    {
		if (Mage::registry('current_category'))
		{
			if ($this->_getData('creare_category_heading'))
			{
				return $this->getCreareCategoryHeading();
			} else
			{
			return parent::getName();
			}
		} else
		{
			return parent::getName();
		}
	}

}

Creare/Seoheading/sql/seoheading_setup/mysql4-install-0.0.1.php

My install script creates the Category attribute ‘creare_category_heading’ and puts it in the ‘General Information’ tab. Older versions of Magento use just ‘General’ so have a look and change if necessary.

<?php
/*
* SEO Heading Category Attribute Setup
*/

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();

$installer->addAttribute('catalog_category', 'creare_category_heading', array(
	'group'         => 'General Information',
	'input'         => 'text',
	'type'          => 'varchar',
	'label'         => 'Category Heading',
	'visible'       => 1,
	'required'      => 0,
	'user_defined' => 1,
	'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE
	));

$installer->endSetup();

Creare/Seoheading/etc/config.xml

For those interested, my config.xml:

<?xml version="1.0"?>
<config>
  <modules>
    <Creare_Seoheading>
      <version>0.0.1</version>
    </Creare_Seoheading>
  </modules>
  <global>
    <models>
      <seoheading>
        <class>Creare_Seoheading_Model</class>
      </seoheading>
      <catalog>
        <rewrite>
          <category>Creare_Seoheading_Model_Category</category>
        </rewrite>
      </catalog>
    </models>
    <resources>
      <seoheading_setup>
        <setup>
          <module>Creare_Seoheading</module>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </seoheading_setup>
      <seoheading_write>
        <connection>
          <use>core_write</use>
        </connection>
      </seoheading_write>
      <seoheading_read>
        <connection>
          <use>core_read</use>
        </connection>
      </seoheading_read>
    </resources>
  </global>
</config>

The end result is an extra category attribute for you to use to your leisure.

Category Attribute

Thanks for reading, if anyone has any suggestions or improvements I’d be glad to hear your feedback. Yyou can download the module zip here: Creare_Seoheading.zip

By Adam Moss

Adam is Ecommerce Manager and a PHP developer at Creare Group. Adam is responsible for training Magento development within the company. Follow Adam on Twitter: http://twitter.com/adampmoss. - .

Post A Comment

Your comments:
Enclose code snippets within the appropriate tags: [php][/php]   [js][/js]   [xml][/xml]   [css][/css]   [html][/html]
E.g: [php]<?php echo "hello world"; ?>[/php]

Search Blog

Follow us on Twitter

Archives

For the record...

Views & opinions in this blog are those of the individual and do not necessarily reflect those of E-commerce Web Design or the Creare Group.