How to Hide a Module in Articles but Show It in the Parent Category

Joomla is a very powerful CMS (much more powerful than WordPress, in our humble opinion), but there are some basic things that cannot be done easily. For example, one of our clients told us that they wanted to display a few modules only on the category blog page, but not on the child categories. They had a category called Vacations, and articles such as Amsterdam, London, Paris, etc… and they only wanted to display a few modules on the Vacations category blog page, but not on the Paris page.

Those who have been using Joomla for a long time know how modules are displayed on a page: they are assigned to a Menu Item Id, and they are displayed only for that Menu Item Id. In a default installation of Joomla and for articles who don’t have unique Menu Items, the Menu Item Id of those articles is the same as the parent category, which means that the modules appearing on the parent category will also display on those articles.

But, if what if you don’t want to display those modules on child articles?

Unfortunately, there’s no way from within Joomla to do this. However, you can do this by making adjustments to your template. Here’s how:

  • You create module positions in the template that should show up only on the category page.
  • You add a condition, in the template, to display these modules on category pages, and not on article pages.

Here’s how to do this technically:

  • Open the file index.php located under the templates/[template-name] folder on your Joomla website.
  • In the desired location, add the following code:

    <?php if (JRequest::getVar( 'view' ) != 'article') : ?>
    <jdoc:include type="modules" name="[module-position-name]" />
    <?php endif; ?>

  • Upload the index.php back to the templates/[template-name] folder.

  • Assign your modules to the [module-position-name].

  • Browse your website and you will notice that these modules will not appear on article pages, just category pages.

Is there another way for doing this?

If you’re using a basic Joomla instance, then no, there isn’t any other way. However, if you’re using sh404SEF, then there are other ways to do this, but not as clean or stable (basically you will need to modify the ItemID in the category/article URLs. Works but not a great idea and not something that we recommend).

If you want to do the above but you lack the technical skills, then all you need to do is to contact us. We can do this for you in no time, we will not charge you much, and we are really, really friendly!

7 Responses to “How to Hide a Module in Articles but Show It in the Parent Category”
  1. Comment by Josh Lewis — July 22, 2015 @ 12:13 am

    Awesome article! I am so happy I was able to find this. It works like a charm on Joomla 3.4.3. I suggest removing the [brackets] from the code to prevent confusion. At first I wasn’t sure if it needed to access an array (which sounded unusual but I went with it) but it turns out the brackets broke the code. Removing them fixed it of course.

    You guys made my night!

  2. Comment by Hugo Fittipaldi — October 15, 2015 @ 4:00 pm

    Hi. I have another question: I have a category named Disease and I want to show a module just in one of the child articles. I mean, not in the article, but on right side of the page. How can I do that?

  3. Comment by Fadi — October 21, 2015 @ 5:51 am

    Hi Hugo,

    Yes, you can show a module exclusively on one article. You can do this by replacing the following line in the code above:

    <?php if (JRequest::getVar( 'view' ) != 'article') : ?>

    with:

    <?php if (JRequest::getVar( 'view' ) == 'article' && JRequest::getVar('id') == 'The ID of the Article that you want to show the module on') : ?>

  4. Comment by Daniel — February 12, 2016 @ 6:43 am

    Hello,

    Will this code work for K2 because I want to hide a module in K2 items but I want to show it in the category. If it doesn’t work, could you provide me with working code?

  5. Comment by Fadi — February 15, 2016 @ 11:41 pm

    Hi Daniel,

    You can replace the above code with:

    <?php if (JRequest::getVar( 'view' ) != 'article') : ?>
    <jdoc:include type="modules" name="[module-position-name]" />
    <?php endif; ?>

    (Notice that “article” was replaced with “item”).

  6. Comment by Lena — October 25, 2017 @ 10:52 am

    Hi guys,

    I have a vice versa situation, I need to display a module on every article page of the category, but not on the main page. So let’s say the category is special events with list of upcoming events, and when you go into each event it will display that module. Is there a way to do that?

  7. Comment by Fadi — October 25, 2017 @ 1:32 pm

    Hi Lena,

    Just replace:

    <?php if (JRequest::getVar( 'view' ) != 'article') : ?>

    with:

    <?php if (JRequest::getVar( 'view' ) == 'article') : ?>

Leave a comment