Showing something on the homepage only in Magento

by admin on September 28, 2010

Sometimes when building a Magento theme you may need to show something on the homepage only. This may be the welcome message in the header, or anything else for that matter which is in a PHTML file. PHTML files which are used site wide like the header or footer will by default appear throughout your Magento site.

In this example we will add some code to only show the welcome message on the homepage. We will do this using PHP. This example can be applied to any element in a PHTML file.

The default welcome message appears on the website using PHP in the header.phtml file. The actual text for the message itself is located in the Magento admin panel. (System > Configuration > Design).

In your theme folder you will need to make sure you have the header.phtml file. This will be located in theme name > template > page > html

Open up the file and look for:

 <p class="welcome-msg"><?php echo $this->getWelcome()?></p>

This is the code which pulls the text from the Magento admin panel and displays it on the website.

Now to add the PHP to show this on the homepage only:

<?php if ($this->getIsHomePage()):?>
<p class="welcome-msg"><?php echo $this->getWelcome()?></p>
<?php endif?>

Wrapping any code within a PHTML file in these PHP if tags will show up on the homepage only.

{ 2 comments… read them below or add one }

badmash October 22, 2010 at 7:23 pm

I just signed up to your blogs rss feed. Will you post more on this subject?

Reply

admin October 23, 2010 at 2:47 pm

My intention was to do a series of posts all containing useful Magento snippets that I write as I build themes. This way I can use it as a reference library myself.

Unfortunately recently I have not had time but do have a series of posts ready to write which will appear here shortly…

Reply

Leave a Comment

Previous post:

Next post: