Home » Magento

Magento: Set/Change page layout, title tag, meta keywords and description

16 February 2010 11,408 views Popularity: 23% Share/Bookmark

email

Earlier in this blog, I had written about setting title, keywords, and description from xml layout file.

In this article, I will be showing, how you can set/change title, keywords and description of any page programmatically. I mean, by php code. Here is the layout XML file. I have set title and template in the layout file. I am going to change the title and template of the page from php code. I will also be adding meta keywords and description for the page from php code.

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
		<reference name="head">
			<action method="setTitle"><title>My Module Page</title></action>
		</reference>
		<reference name="root">
            <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
        </reference>
        <reference name="right">
            <block type="modulename/blockname" name="mymodule.sidebar" template="moduletemplatepath/filename.phtml"/>
        </reference>
    </default>
</layout>

Here is the php code to set template, title tag, meta keywords and description. This code can be kept in any php or phtml file.

$root = $this->getLayout()->getBlock('root');
$template = "page/3columns.phtml";
$root->setTemplate($template);

$head = $this->getLayout()->getBlock('head');
$head->setTitle("Your Title");
$head->setKeywords("your, keywords, anything");
$head->setDescription("Your Description");

Now, the title of the page will be changed from ‘My Module Page’ to ‘Your Title’. The meta keywords and description will be set. And the page layout will be changed from 2 columns with right bar to 3 columns.

Related posts:

  1. Magento: Set title, keywords and description in your module
  2. Dynamically change page title and meta tags in C#
  3. Magento: How to change default page layout?
  4. What are Meta-Tags? Full Description of Meta tags
  5. Magento: Show Currency Selector in right sidebar
  6. Magento: Show/Hide website to search engines by adjusting robots meta tag
  7. Magento: How to change or reorder top links?
  8. Displaying all products and new products listing in column/grid layout – Zen-cart
  9. jQuery: Grey out background and preview image as popup
  10. Magento: Easily add breadcrumbs to any page
  • jignesh

    Hello Sir,
    Nice Article.
    Can u please help me out.
    I want to create my own category module for the news site.
    Which files are required from the existing category module from the magento.
    can u list out the file name for me. my database table name is content_category_entity this is eav table.

    Please help me out.

    Thanks in advance

    Regards,
    Jignesh

  • jignesh

    Hello Sir,
    If u have time then please post the article to create a own category module and lists all the required file from the existing magento category module.

    Please post this article in this blog as soon as possible.

    Regards,

    Jignesh

  • http://www.drakz.com/online/keywords/?p=4833 Magento: Set/Change page layout, title tag, meta keywords and … | Drakz Free Online Service

    [...] Magento: Set/Change page layout, title tag, meta keywords and … Share and [...]

  • Fredd

    Hi,

    I tried this in the …/template/catalog/product/view.phtml and it didn’t worked.
    Am I missing something?

    Thx.

  • Sunil Patel

    Hi,
    I am tried this thing on view page of catalog product but that not work …
    Please help me..

    Thanks
    Sunil

  • Rkjha

    Type your comment here.bfnbvTahaHabibur
    Milan
    MilanPattanapong
    SparshBikramTummala
    vbnvHabiburKara