Magento – ( Static ) Show block only on homepage

Who certain block on only the HOME Page So homepage the Magento Stores wants to show, can do this with a small code snippets.

In the header.phtml in the folder

app/design/frontend/base/default/template/page/html

is in the default template or easy:

<?php if ($this->getIsHomePage()):?>
<h1 class="logo"><strong><?php echo $this->getLogoAlt() ?></strong><a href="<?php echo $this->getUrl('') ?>" title="<?php echo $this->getLogoAlt() ?>" class ="logo"><img src ="<?php echo $this->getLogoSrc() ?>" alt ="<?php echo $this->getLogoAlt() ?>" /></a></h1>
<?php else:?>
<a href="<?php echo $this->getUrl('') ?>" title="<?php echo $this->getLogoAlt() ?>" class ="logo"><strong><?php echo $this->getLogoAlt() ?></strong><img src ="<?php echo $this->getLogoSrc() ?>" alt ="<?php echo $this->getLogoAlt() ?>" /></a>
<?php endif?>

and told the white side, whether it is the home / Start or not. Unfortunately, this code can now not so easy to copy anywhere, at the same To obtain result.

A solution, just to display, whether it concerns the “Home” Page acting, would e.g.:

<?php
$routeName = Mage::app()->getRequest()->getRouteName();
$identifier = Mage::getSingleton(\'cms/page\')->getIdentifier();

if($routeName ==  cms  && $identifier == \'home\') {
echo \'You are in Homepage!\';
} else {
echo \'You are NOT in Homepage!\';
}
?>

This also takes into account different languages.

Another possibility, to this would be to check:

<?php
$is_homepage = Mage::getBlockSingleton('page/html_header')->getIsHomePage();
if($is_homepage)
{
echo "This is the homepage!";
} else {
echo "This is NOT the home page!";
}
?>

So, But back to the question how one can (static) Block only on the homepage show can be and not just the header. We wanted this e.g.. insert in the footer. So from SEO reasons, certain footer text and a code ( HERE ) only displayed on the start page / be used.

We then used the following code, was placed in the in the right place footer.phtml:

<?php
$ref = new Mage_Page_Block_Html_Header();
if($ref->getIsHomePage()){ ?>
<div class ="homepage"><?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('Ihr_Block_Seitenbezeichner')->toHtml();?></div>
<?php
}
?>

(There is also the possibility of turning this and to tell the system “If you are NOT the Home Page, then do X” purpose the upper part of one snippet! insert, so then it looks like:

<?php
$ref = new Mage_Page_Block_Html_Header();
if(!$ref->getIsHomePage()){ ?>
<div class ="homepage"><?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('Ihr_Block_Seitenbezeichner')->toHtml();?></div>
<?php
}
?>

It is then shown on all sides of the block, not only on the Home Page.)

Used in Magento versions 1.4.1.1 bus 1.7.0.2.. Ask, Comments, Suggestions? Gladly!

Published by Covos

Since 2009 I have been working intensively with Magento. I started with the creation and operation of B2C stores. This was extended through my work in the logistics sector. This resulted first specialized B2E systems. Today I work day-a day with exciting B2C, B2B- and B2E projects and reports in this blog about challenges and give insider tips.

Leave a Reply

Your email address will not be published. Required fields are marked *