Display/hide a module by criteria other than menu item

Print

We have used a number of modules which we want to display or hide by criteria other than the Joomla default control of attaching the module to specific menu items. For example, we use Jaggy Blog which provides a module for our blog categories. We want the blog tag words to appear when the blog is being viewed, but not on the rest of the site. Since the categories are delivered through the module, there is no menu item to attach the tag word module to.

Here is a way to accomplish this. First, you need some PHP criteria to identify the page you want to display or hide the module on. Next you need a couple of plugins. We are using mod_PHP and Modules Anywhere.

mod_PHP allows you to create modules that include code like Javascript and PHP code. Being able to include PHP code is essential. Modules Anywhere allows you to include modules in various place on your site including content areas and in this case, inside another module.

For the criteria, in our case, it was the GET value of view which equals Jaggyblog when we want the module to display. If you are using SEF links, you may need to hack you template to temporarily display GET values, or look at the link on a menu item or in you SEF listing if you are using a 3rd party extension. In our case we created a couple of menu items for the blog to find the parameter we needed.

Next create the module that you want to include and attach it to no menu items:

Create the module to be included.

Next you need to create the module that will include the module we just created. Notice that we called the previous module a name which ended in "Include." because it won't be displayed on its own, but rather wrapped in the module we are about to create. We create a mod_PHP module (notice because mod_PHP was based on mod_HTML, it still says modHTML in the module type) which we called the same name as the previous module without the "Include."

Create the modPHP module.

In the first module, the position and order don't matter because it is not assigned to any menu items. This module (mod_PHP) needs to be assigned to all menu items. That tells Joomla it should always be displayed. Here is the trick. If the module contents is empty, it won't display. In our cases so far, the title bar does not display when using this technique, so if you use a module included this way and it shows up on every page as an empty module with a title bar, please let us know so we can take a look at it.

Now in the contents of the module use the PHP criteria you devised to display the contents only if your criteria is met. In our case, it was if ($_GET['view']=="jaggyblog") {

Set the cirteria using PHP

And there you go. The module only displays when the view is of the type we want. The div tag with the id of blogTags is optional. We included that so we could get the title element back. Next time we'll describe how this was done.