I'm trying to include a graphic on all category pages except one specific category page and all of its children category pages too.

I am successfully excluding it from the main category 348, but I'd like to exclude from all of its children as well.

Here's the code I currently have:

        <?php // Steps Graphic 
        if($level == 3 && $category_id != 348): 
            echo $this->getLayout()->createBlock('cms/block')->setBlockId('step1_graphic')->toHtml();
        elseif ($level == 4 && $category_id != 348):
            echo $this->getLayout()->createBlock('cms/block')->setBlockId('step2_graphic')->toHtml();

Here are the entire script list.phtml that I'm working with:


           * To change this template, choose Tools | Templates
           * and open the template in the editor.
             $_helper    = Mage::helper('catalog/output');
               $parentCollection =$catalogx->getCurrentChildCategories();

             <?php if($count>0):?>
                   <?php $_columnCount = 4;
                      $product = Mage::getModel('catalog/product')->load(0);
                      $catThumbPlaceholder = Mage::helper('catalog/image')->init($product, 'image')->resize(99, 161); ?>
             <div class="category-products subcategory-catview">
                         <?php $category_id= Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
              $rootCategoryId = Mage::app()->getStore()->getRootCategoryId();?> 
              <?php if($category_id!=$rootCategoryId): ?>
              <?php $_category= Mage::getModel('catalog/category')->load($category_id);
              <?php //echo Mage::getModel('catalog/layer')->getCurrentCategory()->getName(); ?>
              <div class="page-title category-title">
                  <?php if($level==2): $buttontext='Select Type';?>
                   <h1><?php echo 'Select Door Type'; ?></h1>
                  <?php else: $buttontext='Select Style'; ?>
                    <h1><?php echo 'Select '.$_category->getName(); ?></h1>
                  <?php endif; ?>

              <div class="category-short-description std">
              <?php echo $_category->getShortDesciption();?>
                  <?php //echo $_helper->categoryAttribute($_category, $_short_description, 'short_description') ?>

                  <?php // Steps Graphic 
                      if($level == 3 && $category_id != 348): 
                          echo $this->getLayout()->createBlock('cms/block')->setBlockId('step1_graphic')->toHtml();
                      elseif ($level == 4 && $category_id != 348):
                          echo $this->getLayout()->createBlock('cms/block')->setBlockId('step2_graphic')->toHtml();
                  <?php endif;?> 

              <?php endif; ?>
                     <?php $i=0; foreach ($parentCollection as $parent): ?>
                              <?php if ($i++%$_columnCount==0): ?>
                             <ul class="products-grid subcategory-list">
                             <?php endif;?>

                                                 <li class="item<?php if ($i == 1 || $i%4 == 1): ?> first<?php elseif ($i == $count): ?> last<?php endif; ?>">
                                                     <div class="category-entry">
                                                           <div class="pro-image">
                                                         if ($parent->getThumbnail()){
                                                             $parentThumb = Mage::helper('catalogx')->resizeImage($parent->getThumbnail(), 'catalog' . DS . 'category', 99, 161);
                                                             $parentThumb = $catThumbPlaceholder;
                                                          <a href="<?php echo $parent->getUrl() ?>" title="<?php echo $parent->getName() ?>" class="product-image"><img src="<?php echo $parentThumb ?>" width="99" height="161" alt="<?php echo $parent->getName() ?>" /></a>

                                                             <div class="pro-text cat-text">
                                                                 <?php $catname=$parent->getName(); ?>
                                                              <h2 class="product-name"><a href="<?php echo $parent->getUrl() ?>" title="<?php echo $parent->getName() ?>">
                                                               <?php echo $catname ?></a></h2>
                                                          <div class="actions">
                                                              <button type="button" title="<?php echo $buttontext ?>" class="button btn-cart" onclick="setLocation('<?php echo $parent->getUrl() ?>')"><span><span><?php echo $buttontext; ?></span></span></button>



                               <?php if ($i%$_columnCount==0 || $i==$count): ?>
                              <?php endif;?> 

               <?php $j++; endforeach; ?>
          <?php endif; ?>

Any help is much appreciated!


You could do this in CSS to just hide the image.

However I personally would consider creating a theme where the theme has just one file - a different list.phtml. Then, for the category at the top of the tree where you want this alternate view specify this alternate theme. Set the child categories to use the parent theme with the options in the design/layout. You should just need to do this at the top of the tree with the theme over-ride cascading to child categories.

The template files for your 'new theme' will be from your main theme with the exception of your list.phtml. Doing it this way is how Magento is designed.

Another approach is to add a category attribute, however that is not as easy to do or maintain.

You also want to avoid 'if category = nnn or ooo or ppp' logic in your template because it is not so easy to maintain.


I think you're trying to do it fundamentally wrong; it can be done without changing any code using custom layout updates.

Instead of hardcoding blocks in templates, it's always good idea to declare them in layout and use that declaration instead. It allows you to change layout later on, remove or add stuff and so on. Here's an example how to define footer block on EVERY page using local layout update file (local.xml):

<?xml version="1.0" encoding="UTF-8"?>
        <reference name="footer">
            <block type="cms/block" name="static-block">
                <action method="setBlockId">

To call your block in template use following PHP code:

<?php echo $this->getChildHtml('static-block'); ?>

Now if you want to remove this block from a certain category, go to your chosen category, open Custom Design tab and enter following to Custom Layout Update:

<remove name="static-block" /> 

The line above removes block definition from layout and the PHP code before it displays nothing.

Before you try out those snippets, remember that default context tag applies to ALL pages. Since you wish those block appear only on category pages, you probably want to use catalog_category_default and/or catalog_category_layered instead. And instead of footer you probably want to reference content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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