Show Joomla module only in specific articles?
-
04-03-2021 - |
Question
Is it possible to show modules in Joomla only in a specific article (not per menu item), but in standard module position?
For example somehow get the current article id in a template and insert the modules with according id suffix in the name?
Solution
I would advise you not to hardcode things like this in the template. My question is, why don't you want to use a menu item? You can create a hidden menu item for that article and use it, then assign the module to that menu item. If you still want to do it without using a menu item, a possible workaround would be to use something like "mod_php" (some module that allows you to use php code) and do something more or less like this:
- Create the module and assign it to a position that is not used anywhere (you can type whatever you want in the module position)
In your php module, put this code:
$option = JRequest::getVar( 'option', '' ); $view = JRequest::getVar( 'view', '' ); $id = JRequest::getInt( 'id', 0 ); if ( $option == "com_content" && $view == "article" && $id == YOUR_ARTICLE_ID ) { $module = JModuleHelper::getModule('your_module_type', 'module_title'); if ( ! empty( $module ) ) { $attribs = array( 'style' => 'xhtml' ); echo JModuleHelper::renderModule( $module, $attribs ); } }
I'm sorry if the code snippet is not showing properly, but I hope you can read it ok. Just one thing, when you fill in the part saying 'your_module_type', don't include the "mod_" part of the name. For example, if you want to output a module of type "mod_article_list", you should write "article_list" in "your_module_type".
I hope it helps!