0

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
simekadam
  • 7,334
  • 11
  • 56
  • 79

1 Answers1

2

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:

  1. Create the module and assign it to a position that is not used anywhere (you can type whatever you want in the module position)
  2. 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!

jackJoe
  • 11,078
  • 8
  • 49
  • 64
alghimo
  • 2,899
  • 18
  • 11
  • Thank you for your answer..Looks promising.. Actually I will try the solution with menu items..I did not know that when there is the hidden menu item solution:) As I figured out, the article-menuitem reference works both sides..Thats cool. Going to try that right now! – simekadam Nov 18 '11 at 20:50
  • Hey so I created really simple module with the code you posted and YES its working!thank you.. – simekadam Nov 18 '11 at 22:24
  • Actually I think the second and chosen solution is much cleaner, isnt it? – simekadam Nov 18 '11 at 22:31
  • Nope, I think it's much better the hidden menu item solution. Think for a moment if you don't wanted to print a single module with the article, but 10 different modules.. It would be a matter of just assigning these modules to a menu item, but with the second solution you would have to write 10 php modules.. Anyway, I'm glad it helped – alghimo Nov 19 '11 at 11:25
  • @alghimo in menu assignment setting I must set it to show in all pages? module title is just the name I write in title field? – shekoufeh Jul 02 '16 at 05:29