Engine23
blogBannerImage
By Engine23

Disable Magento block caching for top navigation

Here is the scenario, I was using the topMenu for my main navigation.  I had some custom things being displayed in the UL that was not a catalog/category.  It was hard coded into my version of page/html/topmenu.phtml

$module = Mage::app()->getRequest()->getModuleName();
// Mage_Page_Block_Template_Links

$block = Mage::getBlockSingleton('page/template_links');
$layout = $block->getLayout();
$links = $layout->getBlock('top.links');
$header = Mage::getBlockSingleton('page/html_header');
?>

<?php $_menu = $this->getHtml('level-top') ?>
<?php if($_menu): ?>
<div class="nav-container">
<div id="nav-wrapper">
<ul id="nav">
<li class="level0 nav-1 first <?php echo (($module === 'home') || $this->getIsHomePage())?'active': null; ?> level-top"><a class="level-top" href="<?php echo $this->getUrl('home'); ?>"><span>Home</span></a></li>
<li class="level0 nav-1 first <?php echo ($module === 'contacts')?'active': null; ?> level-top"><a class="level-top" href="<?php echo $this->getUrl('contacts'); ?>"><span>Contact Me</span></a></li>
<?php echo $_menu ?>
<li class="level0 nav-1 first <?php echo ($module === 'blog')?'active': null; ?> level-top"><a class="level-top" href="<?php echo $this->getUrl('blog'); ?>"><span>Blog</span></a></li>
<li style="width: 180px; display: block;" class="level0 nav-1 first level-top"><a class="level-top" href="<?php echo $this->getUrl('blog'); ?>"><span></span></a></li>
<li class="level0 nav-1 first <?php echo ($module === 'geek')?'active': null; ?> level-top"><a class="level-top" href="<?php echo $this->getUrl('geek'); ?>"><span>Geek Check</span></a></li>
<li class="level0 nav-1 first <?php echo ($module === 'tag')?'active': null; ?> level-top"><a class="level-top" href="<?php echo $this->getUrl('tag'); ?>"><span>Tags</span></a></li>
<li class="level0 nav-1 first <?php echo ($module === 'portfolio')?'active': null; ?> level-top"><a class="level-top" href="<?php echo $this->getUrl('portfolio'); ?>"><span>Portfolio</span></a></li>
<li class="level0 nav-1 first <?php echo (($module === 'customer') || ($module === 'checkout'))?'active': null; ?> level-top"><a class="level-top" href="javascript: void(0);"><span>Customer</span></a>
<?php $_links = $links->getLinks(); ?>
<?php if(count($_links)>0): ?>
<ul class="level0"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>>
<?php $i = 1; ?>
<?php foreach($_links as $_link): ?>
<?php if ($_link instanceof Mage_Core_Block_Abstract):?>
<?php echo $_link->toHtml() ?>
<?php else: ?>
<li class="level1 nav-1-<?php echo $i; ?> <?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>" <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><span><?php echo $_link->getLabel() ?></span></a><?php echo $_link->getAfterText() ?></li>
<?php endif;?>
<?php $i++; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
</ul>
</div>
</div>
<?php endif ?>

Very simple process is to modify your design local.xml here is what it may look like: 

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
<default>

<reference name="catalog.topnav">
<action method="unsetData"><key>cache_lifetime</key></action>
<action method="unsetData"><key>cache_tags</key></action>
</reference>

</default>

</layout>