Control modules based on article category

(16 votes, average 4.25 out of 5)

With these recipes you can make modules appear/disappear when an article from a specific section or category is shown. They even detect section/category blog/list pages and will show the module on those pages if the section/category matches.

With JomGenius

This method is available from MetaMod 2.4c onwards. Each of these recipes is just 1 line. Pick the recipe that best suits your needs and just replace the UUU-ZZZ at the end of the line with the module id(s) or module position that you want to return.

Category detection:

// This rule will trigger on category pages (blog or list style) as well as article
// pages, where the category id of the category or article matches the one shown
if ( $content_genius->check("category_id = 15") ) return VVV;
if ( $content_genius->check("category_id = 17") ) return WWW;
// combine several categories for one module id:
if ( $content_genius->check("category_id = 18,19,20") ) return XXX;
// exclude category or categories
if ( $content_genius->check("category_id not = any of 15,16,17") ) return UUU;
if ( $content_genius->check("category_id not = 18") ) return UUU;
// show certain modules only on article pages where the article is in a certain category
if ( $content_genius->check("pagetype = article")
    and $content_genius->check("category_id = 15") ) return UUU;
// show certain modules only on category blog pages where the article is in a certain category
if ( $content_genius->check("pagetype = categoryblog")
    and $content_genius->check("category_id = 15") ) return UUU;
// show certain modules only on category list pages where the article is in a certain category
if ( $content_genius->check("pagetype = categorylist")
    and $content_genius->check("category_id = 15") ) return UUU;

Section Detection:

// This rule will trigger on section pages (blog or list style) as well as category pages and article
// pages, where the section id of the section, category or article matches the one shown
if ( $content_genius->check("section_id = 15") ) return VVV;
if ( $content_genius->check("section_id = 17") ) return WWW;
// combine several sections for one module id:
if ( $content_genius->check("section_id = 18,19,20") ) return XXX;
// exclude section or sections
if ( $content_genius->check("section_id not = any of 15,16,17") ) return UUU;
if ( $content_genius->check("section_id not = 18") ) return UUU;
// show certain modules only on article pages where the article is in a certain section
if ( $content_genius->check("pagetype = article")
    and $content_genius->check("section_id = 15") ) return UUU;
// show certain modules only on category blog pages where the article is in a certain section
if ( $content_genius->check("pagetype = categoryblog")
    and $content_genius->check("section_id = 15") ) return UUU;
// show certain modules only on category list pages where the article is in a certain section
if ( $content_genius->check("pagetype = categorylist")
    and $content_genius->check("section_id = 15") ) return UUU;
// show certain modules only on section blog pages where the article is in a certain section
if ( $content_genius->check("pagetype = sectionblog")
    and $content_genius->check("section_id = 15") ) return UUU;
// show certain modules only on section list pages where the article is in a certain section
if ( $content_genius->check("pagetype = sectionlist")
    and $content_genius->check("section_id = 15") ) return UUU;

Pre-JomGenius

If you have MetaMod 2.4 onwards, try the JomGenius version (in the other tab). It's much simpler.

Category detection:

$category_id = null;
if ( $option == "com_content" ) {
  if ( $view == "category" ) {
   /* category list pages (blog or list style) */
   $category_id = (int)$id;
  } else if (array_key_exists("catid",$_REQUEST)) {
   /* if the category id is in the URL */
   $category_id = (int)JRequest::getInt("catid",0);
  }
  if ( $category_id === null && $view == "article" ) {
   /* if it's an article page without the catid mentioned in the url */
   $nullDate = $db->Quote( $db->getNullDate() );
   $my_id = $db->Quote( $db->getEscaped( (int)$id ) );
   $jnow  =& JFactory::getDate();
   $now    = $db->Quote( $db->getEscaped( $jnow->toMySQL() ) );
   $query = "SELECT  title,  id,  catid "
       . " FROM #__content WHERE id = $my_id AND state = 1"
       . " AND ( publish_up = $nullDate "
       . " OR publish_up <= $now )"
       . " AND ( publish_down = $nullDate "
       . " OR publish_down >= $now  )";
   $db->setQuery( $query, 0, 1 );
   $row = $db->loadObject();
   $category_id = $row->catid;
  }
}
/* Now customise any of the following rules for your use.
 * $category_id will correspond to the category id of the
 * article being displayed (if one is being displayed!)
 */
if ($category_id == 1) return 55;/* on category 1, return module 55 */
if ($category_id == 2) return 56;/* on category 2, return module 56 */
if ($category_id == 3) return 57;/* on category 3, return module 57 */
return 50; /* default if nothing else matches */

Section Detection: (updated 6 October 2009 due to bugs in previous version)

if ( $option == 'com_content' &amp;&amp; $view != 'frontpage' ) {
  $section_id = null;
  if ( $view == 'section' ) {
   /* section list pages (blog or list style) */
   $section_id = (int)$id;
  } else if (array_key_exists('sectionid',$_REQUEST)) {
   /* if the section id is in the URL */
   $section_id = (int)JRequest::getInt('sectionid',0);
  }
  if ( $view == 'category' ) {
   /* if it's a category list or blog page, get the section that the
    * category is in
    */
   $my_id = $db->Quote( $db->getEscaped( (int)$id ) );
   $query = "SELECT section from #__categories where id = $my_id";
   $db->setQuery( $query, 0, 1 );
   $row = $db->loadObject();
   $section_id = (int)( $row->section );
  }
  if ( $section_id === null &amp;&amp; $view == 'article' ) {
   /* if it's an article page without the sectionid mentioned in the
    * url, look up the section id for that article
    */
   $nullDate = $db->Quote($db->getNullDate());
   $my_id = $db->Quote( $db->getEscaped( (int)$id ) );
   $jnow  =&amp; JFactory::getDate();
   $now   = $db->Quote( $db->getEscaped( $jnow->toMySQL() ) );
   $query = "SELECT  title,  id,  sectionid "
       . " FROM #__content WHERE id = $my_id AND state = 1"
       . " AND ( publish_up = $nullDate "
       . " OR publish_up <= $now )"
       . " AND ( publish_down = $nullDate "
       . " OR publish_down >= $now  )";
   $db->setQuery( $query, 0, 1 );
   $row = $db->loadObject();
   $section_id = $row->sectionid;
  }
 
  /* Now customise any of the following rules for your use.
   * $section_id will correspond to the section id of the
   * article being displayed (if one is being displayed!)
   */
  if ($section_id == 0) return 54;/* uncategorised, return module 54 */
  if ($section_id == 1) return 55;/* on section 1, return module 55 */
  if ($section_id == 2) return 56;/* on section 2, return module 56 */
  if ($section_id == 3) return 57;/* on section 3, return module 57 */
}
return 50; /* default if nothing else matches */