Sat 20 Feb 2021
![Man drinking coffee while reading about joomla on a laptop](https://i0.wp.com/offerpalmedia.com/wp-content/uploads/2021/02/modules-nowhere.jpg?resize=790%2C420&ssl=1)
Unlike many other CMS that are page based, Joomla is much more dynamic with everything based on the menu item. When you select a menu item Joomla decides which view to display, which blocks to display and even which theme.
One of the major advantages of this approach is that content can inherit the settings of a parent so you don’t have to design every page. At the same time this inheritance can be source of confusion, despair and even anger.
The front page of this web site is a view comprising the introductory text from the latest blog posts plus some blocks around the edges. These blocks are connected to the menu (in this case Home) and will display on any “page” that is using the same menu link.
So you will see that on this site there is no menu link for each post. The post just inherits the settings of the page (menu link) with the introductory text. In Joomla this page (menu link) is called a “blog category view” where I configured the layout of the page and then associated the “modules” (Joomla term for the blocks) to that page. This saves me a lot of time when building the site and ensures a consistent look and feel.
But what if?
However there are times where you want to display a module only on the parent (the page with the intros) or only on the children (the page with the full article). Out of the box you just can’t do that. The children have the same modules as the parent. There are a few extensions available that work around this potential limitation but I always prefer to build as much as possible of the site from just the core. It makes upgrades and security much easier to maintain – and I like an easy life. And as you will see – why install an extension when it’s such a quick and easy change.
As I was building a https://learnjoomla4.com I really needed to have a module displayed only on the parent but not on the children. I might have been able to create some complicated menu structure with hidden menu links or install an extension but this time I decided to see if there was another way. Spoiler alert there is.
If you have ever built a Joomla template or even just taken a peek inside you will be familiar with the following code.
<?php if ($this->countModules('sidebar', true)) : ?> <div> <jdoc:include type="modules" name="sidebar" style="default" /> </div>
<?php endif; ?>
In simple terms it is checking to see if there are any modules configured to be displayed in a position in the template called “sidebar”. If there are then they are displayed. If not then the code is ignored.
Remember that modules can only be configured to display on selected menu items. But as I described before if my menu item is a category blog view then any modules I place on that view will be repeated on the children which are dynamically created without a menu link.
The following code will resolve this for you and you can easily add it to your own Joomla template using the built in template editor.
<?php if ($view != 'article' && $this->countModules('sidebar', true)) : ?> <div> <jdoc:include type="modules" name="sidebar" style="default" /> <div>
<?php endif; ?>
Here you can see that I have added an additional condition to the “if statement”. I am now checking both for the existence of modules but also what type of “view” I am currently on.
- The view for a category blog page is “category”
- The view for an article is “article”
As a result by checking that the view is not (!=) an article I am preventing the sidebar modules from being displayed on the full article view but keeping it displayed on the category blog page. Of course you can also change from “not equal(!=) article” to “not equal(!=) category” which will then display the module only on the child pages.
I don’t remember if I have read this before and forgotten about it or not.
On the basis that I must have just forgotten about it I am writing this post not just to share it with you but also as a reminder to myself.