Placing modules on various MyBlog pages

(2 votes, average 3.00 out of 5)

MyBlog is a great blogging extension for Joomla – highly recommended. You can use the following recipes to target modules onto different pages within the extension.

Detect the categories (“tags”) from posts

This recipe allows you to place a particular module on every post that is tagged with a particular tag. Because each post can have more than one tag, this recipe is set up so that if you detect more than one tag, you can add modules for each detected tag.

if ($option == 'com_myblog') {
  if ( ($show = JRequest::getVar( 'show' ) ) != '') {
    $query = 'SELECT c.name from #__myblog_permalinks pl
      LEFT JOIN #__myblog_content_categories cc on pl.contentid = cc.contentid
      LEFT JOIN #__myblog_categories c on cc.category = c.id
      WHERE pl.permalink = ' . $db->quote($db->getEscaped( $show ));
    $db->setQuery( $query );
    $res = $db->loadResultArray();
    $mods = array();
    // was sports one of the tags? if so add mod 101
    if ( array_search('sports', $res) !== false ) $mods[] = 101;
    // was hockey one of the tags? if so add mod 102
    if ( array_search('hockey', $res) !== false ) $mods[] = 102;
    return $mods;
  }
}
 

Detect the “tag” from a category listing by tag

This recipe detects the tag name, when the viewer is viewing a page like http://www.example.com/blog/tags/sports.

if ($option == 'com_myblog' and JRequest::getVar('task') == 'tag') {
  $tag = JRequest::getVar('category');
  if ( $tag == 'sports' ) return 101; /* replace 101 with module to show for sports tag */
  if ( $tag == 'hockey' ) return 102; /* replace 102 with module to show for hockey tag */
}

If you want your tag-related modules to show up for certain tags on both “tag listing” pages and individual posts that have that tag, you can combine the two rules:

if ($option == 'com_myblog') {
  $mods = array();
  $tag = '';
  if ( JRequest::getVar('task') == 'tag') {
    $tag = JRequest::getVar('category');
  }
  else if ( ($show = JRequest::getVar( 'show' ) ) != '') {
    $query = 'SELECT c.name from #__myblog_permalinks pl
      LEFT JOIN #__myblog_content_categories cc on pl.contentid = cc.contentid
      LEFT JOIN #__myblog_categories c on cc.category = c.id
      WHERE pl.permalink = ' . $db->quote($db->getEscaped( $show ));
    $db->setQuery( $query );
    $res = $db->loadResultArray();
  }
  // was sports one of the tags? if so add mod 101
  if ( $tag == 'sports' or array_search('sports', $res) !== false ) $mods[] = 101;
  // was hockey one of the tags? if so add mod 102
  if ( $tag == 'hockey' or array_search('hockey', $res) !== false ) $mods[] = 102;
  return $mods;
}

Detecting other MyBlog page types

The page with the list of tags (e.g. http://www.example.com/blog/tags/)

if ( $option == "com_myblog" and JRequest::getVar('task') == 'categories' ) return 101;

MyBlog “home” page

if ( $option == "com_myblog" and JRequest::getVar('task') == '' ) return 101;

MyBlog search page

if ( $option == "com_myblog" and JRequest::getVar('task') == 'search' ) return 101;

“My Entries” tab on the “My Account” page

if ( $option == "com_myblog" and JRequest::getVar('task') == 'adminhome' ) return 101;

“Preferences” tab on the “My Account” page

if ( $option == "com_myblog" and JRequest::getVar('task') == 'bloggerpref' ) return 101;

Statistics tab on the “My Account” page

if ( $option == "com_myblog" and JRequest::getVar('task') == 'bloggerstats' ) return 101;

The page for an individual blogger, showing their posts. You could show a different module for particular users.

if ( $option == "com_myblog" ) {
  $blogger = JRequest::getVar( 'blogger' );
  if ( $blogger == 'johnB' ) return 101;
  if ( $blogger == 'fayed' ) return 102;
  // add more checks as appropriate...
 
  /* on the blogger page, but not one of the previously detected people: */
  if ( $blogger != '' ) return 199;
}