Difference between revisions of "Show a Module on all Menu Items except selected ones"

From Joomla! Documentation

(new tip on creating conditional positions)
 
m (There were 2 closed brackets missing from the array version of the code. I only figured this out trying the code and struggling with it for 15 minutes! Hope this helps others.)
Line 27: Line 27:
 
     <?php $Itemid = JRequest::getVar('Itemid'); // get the current itemid
 
     <?php $Itemid = JRequest::getVar('Itemid'); // get the current itemid
 
     // don't show this position if $Itemid is 1, 4, or 22
 
     // don't show this position if $Itemid is 1, 4, or 22
     if(($this->countModules('exclude_left')) && !(in_array($Itemid, array(1,4,22)) : ?>  
+
     if(($this->countModules('exclude_left')) && !(in_array($Itemid, array(1,4,22)))) : ?>  
 
         <jdoc:include type="modules" name="exclude_left" style="rounded" />
 
         <jdoc:include type="modules" name="exclude_left" style="rounded" />
 
     <?php endif; ?>
 
     <?php endif; ?>

Revision as of 09:01, 19 October 2009

Overview[edit]

One frequently requested feature (which will be added in version 1.6!) is the ability to show a Module on all Menu Items except for a selected list. That way, for example, if you want to show a Module on every page except your home page, you don't have to remember to assign it to each new Menu Item you create.

This feature does not exist "out of the box" in version 1.5. However, with a little PHP trickery, we can get the exact same result.

Example[edit]

For this example, we are going to assign a Module to every Menu Item except for our home page. We'll use the Joomla! Sample website and the rhuk_milkyway template. Here are the steps:

  1. Create a new template position. We'll call it "exclude_left". We could use an existing template position, but in some cases it might be better to create a new one.
    • Edit the file templates/rhuk_milkyway/templateDetails.xml and add this line after the "breadcrumb" position on line 63:
      <position>exclude_left</position>
      . This adds the new position to the template.
    • Edit the file templates/rhuk_milkyway/index.php and add these lines of code just below the <div id="leftcolumn"> on line 88:
    
    <?php $Itemid = JRequest::getVar('Itemid'); // get the current itemid
    // don't show this position if $Itemid is 1
    if(($this->countModules('exclude_left')) && ($Itemid != 1)) : ?> 
        <jdoc:include type="modules" name="exclude_left" style="rounded" />
    <?php endif; ?>
This code does two things. First, it adds the new position to the template. However, it only displays the new "exclude_left" position when the current Itemid is not equal to "1". (Note that I got this value by going to the Menu Item Manager for the Main Menu and finding the ItemID for the Home Menu Item.)
  1. Now pick a Module that you want to display on all pages except the Home page. I created a new Custom HTML module, but you can use any module. Assign this Module to the new "exclude_left" position and to All Menu Items. This position will not exist in our Home Menu Item, so we can assign the Module to All Menu Items and it still won't show on the Home page.

That's it. Now test out that the module shows up on every page except the home page. If you like, create a new Menu Item and make sure it shows on that one as well.

Other Things You Can Do[edit]

  • Exclude a list of Itemid's: For example, to exclude 1, 4, and 22, you could change the example code to this:
    
    <?php $Itemid = JRequest::getVar('Itemid'); // get the current itemid
    // don't show this position if $Itemid is 1, 4, or 22
    if(($this->countModules('exclude_left')) && !(in_array($Itemid, array(1,4,22)))) : ?> 
        <jdoc:include type="modules" name="exclude_left" style="rounded" />
    <?php endif; ?>
  • Test For Other Conditions: You can use the same type of logic to test for other situations. Here are some examples using the session's "$_GET" variable:
 $option    = JRequest::getVar('option'); // for example, "com_content"
 $view      = JRequest::getVar('view');   // for example, "article"
 $task      = JRequest::getVar('task');   // for example, "edit"
 $layout    = JRequest::getVar('layout'); // for example, "blog"
 $article_id  = JRequest::getVar('id');   // id of the current article being shown
You could also test for a specific user or group of users or a wide variety of other conditions. So, using this type of logic, you can have greater automatic control what Modules show on which pages.