Actions

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

From Joomla! Documentation

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.)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
===Overview===
 
===Overview===
 
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.
 
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===
 
===Example===
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) 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:
# 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.  
+
Create a new template position. We'll call it "x-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: <source lang="xml"><position>exclude_left</position></source>. 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 <code><nowiki><div id="leftcolumn"></nowiki></code> on line 88:
+
<source lang="php">   
+
    <?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; ?>
+
</source>
+
: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.)''
+
<ol start="2">
+
<li>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.</li></ol>
+
  
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.
+
2) Edit the file templates/rhuk_milkyway/templateDetails.xml and add this line after the "breadcrumb" position on line 63: <source lang="xml"><position>x-left</position></source>. This adds the new position to the template.
 
+
#* Edit the file templates/rhuk_milkyway/index.php and change these lines 88-92:
===Other Things You Can Do===
+
* '''Exclude a list of Itemid's:''' For example, to exclude 1, 4, and 22, you could change the example code to this:
+
 
<source lang="php">     
 
<source lang="php">     
    <?php $Itemid = JRequest::getVar('Itemid'); // get the current itemid
+
<div id="leftcolumn">
    // don't show this position if $Itemid is 1, 4, or 22
+
<?php if($this->countModules('left')) : ?>
    if(($this->countModules('exclude_left')) && !(in_array($Itemid, array(1,4,22)))) : ?>  
+
<jdoc:include type="modules" name="left" style="rounded" />
        <jdoc:include type="modules" name="exclude_left" style="rounded" />
+
<?php endif; ?>
    <?php endif; ?>
+
</div>
 
</source>
 
</source>
* '''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:  
+
becomes:  
<source lang="php">
+
<source lang="php">  
$option    = JRequest::getVar('option'); // for example, "com_content"
+
<div id="leftcolumn">
$view      = JRequest::getVar('view');  // for example, "article"
+
<?php if($this->countModules('left') && !$this->countModules('x-left')) : ?>
$task      = JRequest::getVar('task');  // for example, "edit"
+
<jdoc:include type="modules" name="left" style="rounded" />
$layout    = JRequest::getVar('layout'); // for example, "blog"
+
<?php endif; ?>
$article_id  = JRequest::getVar('id');   // id of the current article being shown
+
</div>
 
</source>
 
</source>
: 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.
+
This code does two things. First, it adds the new position to the template. However, this module position will not be visible on the template and is only used to disable the left column when a module is published to the "x-left" position for a specific page
 +
 
 +
3)In order to disable the left column, simply create a custom html module and assign it to position "x-left".  You may title it whatever you like and enter any content you like for the content...this text will not show on the website.  It is recommended to provide a title like "TURN OFF LEFT COLUMN" to properly notify any others who may be working on the website of the module's function.  Then simply assign the module to the pages you do not want to see the left column.
 +
==Additional positions==
 +
You may want to create corresponding disabled positions for each of the module positions you have.  This will allow you to individually turn off any module position on any page of the website.
  
 
[[Category:Tips and tricks]]
 
[[Category:Tips and tricks]]
 
[[Category:Tips and tricks 1.5]]
 
[[Category:Tips and tricks 1.5]]

Latest revision as of 06:56, 3 July 2012

Overview

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.

Example

1) 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: Create a new template position. We'll call it "x-left". We could use an existing template position, but in some cases it might be better to create a new one.

2) Edit the file templates/rhuk_milkyway/templateDetails.xml and add this line after the "breadcrumb" position on line 63:

<position>x-left</position>

. This adds the new position to the template.

    • Edit the file templates/rhuk_milkyway/index.php and change these lines 88-92:
 
<div id="leftcolumn">
<?php if($this->countModules('left')) : ?>
<jdoc:include type="modules" name="left" style="rounded" />
<?php endif; ?>
</div>

becomes:

 
<div id="leftcolumn">
<?php if($this->countModules('left') && !$this->countModules('x-left')) : ?>
<jdoc:include type="modules" name="left" style="rounded" />
<?php endif; ?>
</div>

This code does two things. First, it adds the new position to the template. However, this module position will not be visible on the template and is only used to disable the left column when a module is published to the "x-left" position for a specific page

3)In order to disable the left column, simply create a custom html module and assign it to position "x-left". You may title it whatever you like and enter any content you like for the content...this text will not show on the website. It is recommended to provide a title like "TURN OFF LEFT COLUMN" to properly notify any others who may be working on the website of the module's function. Then simply assign the module to the pages you do not want to see the left column.

Additional positions

You may want to create corresponding disabled positions for each of the module positions you have. This will allow you to individually turn off any module position on any page of the website.