How to Add a Module Position in a Joomla Template Layout File

Note: A pre-requisite to implement the solution in this post is to install and activate the Modules Anywhere plugin by NoNumber. That plugin is much better than Joomla’s own plugin (Content – Load Module) of injecting modules into a page (e.g. {loadposition …} and {loadmodule …}). It’s amazing that Joomla still hasn’t created a reliable plugin that does this simple job.

If you’re comfortable with Joomla development, then you probably know that you can add module positions to your template by adding the following code to the index.php file located under the templates/[template-name] folder:

<jdoc:include type="modules" name="module_position" style="module_style" />

The module_position is the position of the module, such as top. The module_style is the style of the module, such as none (no style) or xhtml (we have discussed module styles in passing here).

But… what if you wanted to add a module position to say, somewhere after the 2nd leading article in a featured view (or between the featured and the intro items)? You just can’t do that in the index.php, you must do it in the default.php file located under the templates/[template-name]/html/com_content/featured folder. But, if you add the above <jdoc… code to that file, then you will notice that the Joomla template parser will not parse it, which means that the <jdoc… code will display, unchanged, on your website’s frontend. Unless your intention is to show your code to your users, then you must find another way to include your module.

Thankfully, we have devised a simple and yet efficient way to address this problem at itoctopus. Here’s how to do it (we will use the above scenario as an example):

  • Open the default.php file located under the templates/template-folder/html/com_content/featured folder.
  • Just below:

    JHtml::addIncludePath(JPATH_COMPONENT . '/helpers');

    Add the following code:

    $arrModules = JModuleHelper::getModules('[position-of-your-module]');
    $arrModuleIds = array();
    for ($i = 0; $i < count($arrModules); $i++){
    	$arrModuleIds[] = $arrModules[$i]->id;

  • Now, in the location where you want to display the module(s) (there may be several modules allocated to the same module position), you will need to add the following code:

    for ($j = 0; $j < count($arrModuleIds); $j++){
    	echo('{module '.$arrModuleIds[$j].'}');
    }

  • Now you will need to save the file and upload it back. You will now notice that your modules are displaying in the location of your choice.

If the above didn’t work for you, then you will need to check for the following:

  • Are you using caching? If yes, then disable it and see if it works.
  • Are you sure you have the Modules Anywhere plugin by NoNumber installed and activated?

  • Are you sure that your module doesn’t modify the rest of the content (there are some complex modules that do that, we have developed quite a few of them for our clients)? An example where a module may modify the rest of the content prior to adding its own code is to add some JavaScript code to the <head>. If that’s the case, then you will need to add a hidden div layer in the index.php file (of your template), and, inside it, include the module using the <jdoc… statement, that’s in addition to doing the above. Of course, this solution might cause other issues, and you may need to modify some code in the module itself.

If you want to display modules in a specific view of your Joomla website, then try the above solution, it should work. If, for any reason, it doesn’t, or if you think it’s too technical for your own taste, then please contact us. We’ll do it for you in no time and for a very, very affordable cost. We’re also the friendliest programmers in the whole wide world!

No comments yet.

Leave a comment