How to use countModules in any Joomla! file

One of the core parts of creating Joomla! templates is using $this->countModules to show or hide code depending on if a module is in a particular position on the page. It is very powerful and it makes your templates very dynamic according to what is happening on the page. So what happens if you need to do this outside of the template index.php file? Save yourself a huge headache and use the following code in any file to check for a module position:

jimport( 'joomla.application.module.helper' );
if(count(JModuleHelper::getModules('left'))) {
	put your code here
}

A very powerful addition to you Joomla! PHP library!

The Core Team
Editorial Staff Members at 'corePHP'
Editorial staff for the Core Technology Blog for 'corePHP' - news, views insights and advice for e-commerce, marketing technology , web design and development.

8 thoughts on “How to use countModules in any Joomla! file

  1. anuj kumar singh

    Thanks for given good information related to joomla.this function/condition is very useful to show or hide code.

  2. RJS

    Great tip. Can this be used in code.php that does not use ” defined( ‘_JEXEC’ ) or die( ‘Restricted access. Direct Access to this location is not allowed.’ );”

  3. kreatif | web development

    Can this be applied to a com_content override file? For example, can i add this here for example?

    template/html/com_content/category/blog.php

    <?php
    jimport( 'joomla.application.module.helper' );
    if(count(JModuleHelper::getModules('MODULE NAME'))) {
    echo '';
    print_r( $module );
    echo ”;
    }
    ?>

    I can´t make it work..

Comments are closed.