Creating Joomla Templates : The "all but..." Module Position Problem

How to set out a Joomla template so that a module can be displayed on all pages but one.

 

Download HD-Framework template

 


ID Selectors | Joomla Content


 


Here’s a problem, you want a module to appear on one page and a similar but different module to appear on all other pages.

Let’s say you have pages A, B & C.
You want a red image to appear in the banner position on page A and a blue image to appear in the banner position on all other pages (B & C).

Modules can be allocated to positions on ALL menu pages, NONE of the pages & ONE OR MORE pages (on the Module Manager’s Menu Assignment list).

 



If you select the red module to appear on ONE page only (A) then that page is sorted.
If you select the blue module to appear on B & C only, then those pages are also sorted.
But as soon as you create a new page (D) it won’t have either red or blue displayed in its banner position.



Likewise if you set the Blue module to display on ALL pages it will appear on any new page (D) but will also display on page A alongside the Red module.



This is particularly problematic if the client is eager to create new pages (other than blog entires) but doesn’t want the hassle of adding modules to pages.
There’s no option to display on ALL pages BUT



The solution lies in using a “CountModules” statement in the template.



You display the unique module (red) in position “banner” on page A
And the generic module (blue) in position “banner_allbut” on all pages.  
You then tweak the template code slightly so that the “banner_allbut” module is only displayed if there is nothing displayed in the “banner” position.



In the template folder’s  index.php file the module position for banner will look something like this…



<div id="banner" ><jdoc:include type="modules" name="banner"/></div><br /><br /></p> <p>Replace it with...</p> <p><br /><br /> <?php if($this->countModules('banner')) : ?><br /> <div id="banner" ><jdoc:include type="modules" name="banner"/></div><br /> <?php else: ?><br /> <div id="banner_allbut" >jdoc:include type="modules" name="banner_allbut"/></div><br /> <?php endif; ?><br /><br /></p> <p>The first line asks if anything is in the “banner” position, if so the second line displays it.<br />The third & fourth line says “otherwise” and displays “banner_allbut” instead.<br /><br /></p>"

Web Design & Development
07845 950063
This email address is being protected from spambots. You need JavaScript enabled to view it.