PHP code snippets to conditionally add things to your template

The Joomla ItemID-related management of modules is a powerful tool, but sometimes you need to show/hide parts of your site on two special cases not covered by this tool: when you need to show/hide things regardless of the active ItemID - for example on all detail listings page of a given component, or when you need to deal with a component which have poor support of ItemIDs - as VirtueMart, which is notoriously misbehaving in this regard.

Here are the most common such tricks I use on almost daily basis, and thought I would list them below should anyone find them useful...

Detecting the frontpage in Joomla 1.0

<?php if ($option == 'com_frontpage' ) {
    // This is the front page
} else {
    // This is not the front page
} ?>

Detecting the homepage in Joomla 1.5+ - the dumb version

If you use the Frontpage component to show as your default page, you can try this to add something to be shown only on your homepage:

<?php if (JRequest::getVar('view')=='frontpage') { ?>
// put the code you want to show only on frontpage view here
<?php } ?>

Detecting the frontpage - the geek version for Joomla 1.5+

<?php $menu = &JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
// Script here will execute only on frontpage.
} ?>

For the multilingual sites you will need an additional check to detect the frontpage for ALL languages

<?php
$menu = JSite::getMenu();
if ($menu->getActive() == $menu->getDefault( 'en-GB' )) {
    echo 'This is the front page';
}
elseif ($menu->getActive() == $menu->getDefault( 'hu-HU' )) {
    echo 'Ez itt az alapértelmezeett oldal';
}
?>

Here might be necessary to add some extra code to detect the homepage for ALL language versions and add a common code:

<?php $menu = JSite::getMenu(); ?>
<?php $lang = JFactory::getLanguage(); ?>
<?php if ($menu->getActive() == $menu->getDefault($lang->getTag())) : ?>
...

The übergeek version of frontpage detection in Joomla 1.7+ (works in Joomla 2.5 too!)

Write a function in the template like this, and use it to detect the frontpage:

function isFrontPage() {    
    $mainframe = JFactory::getApplication();    
    $sef = $mainframe->getCfg( 'sef' );
    $menu = JSite::getMenu();    
    if ( $sef ) {
        $uri = JURI::getInstance()->toString( array( 'path' ) );
        if ( $menu->getActive() === $menu->getDefault() && ( $uri == '/index.php' || $uri == '/' ) ) {
            return true;
        }
    } else {
        $uri = JURI::getInstance()->toString( array( 'query' ) );
        if ( $menu->getActive() === $menu->getDefault() && ( $uri == '' || substr( $uri, 0, 12 ) == '?limitstart=' ) ) {
            return true;
        }
    }
    return false;
}

Detecting the "Featured" items in Joomla 1.7+

<?php if ($_REQUEST[view] == 'featured') { ?>
 //your code here
<?php } else {
//your code here
<?php } ?>

Adding code (as a Java Script snippet for example) for a page with given ItemID

<?php if (JRequest::getInt('Itemid') == your_id) : ?>
   <!-- special javascript -->
<?php endif; ?>

Adding code for a Section Page

 <?php if (JRequest::getVar('view')=='section') { ?>
// put the code for the section view here
<?php } ?>

Let's combine them: code to be shown on a Section page with a particular ItemID

<?php if (JRequest::getVar('view')=='section' && JRequest::getVar('id')==your_id) { ?>
// put the code for the section view here
<?php } ?>

If on section 1, display this data, if on section 2, display that data

<?php
$db = &JFactory::getDBO();
$id = JRequest::getVar('id');
if ( $id ) {
if ( JRequest::getVar('view') == 'section' ) {
$sectionid = $id;
} elseif ( JRequest::getVar('view') == 'category' ) {
$query = 'SELECT section FROM #__categories WHERE id = ' . (int) $id;
$db->setQuery($query, 0, 1);
$sectionid = $db->loadResult();
} elseif ( JRequest::getVar('view') == 'article' ) {
$query = 'SELECT sectionid FROM #__content WHERE id = ' . (int) $id;
$db->setQuery($query, 0, 1);
$sectionid = $db->loadResult();
}
} else {
$sectionid = '';
}
 
if ($sectionid != '' && $sectionid == 2) { ?>
// Code for Section #2 goes here
<?php } elseif ($sectionid != '' && $sectionid == 3) { ?>
// Code for Section #3 goes here
<?php } ?>

Use the menu alias to do tricks on your page

<?php $theMenu = JSite::getMenu();
$theActiveMenu = $theMenu->getActive();
if ( strpos($theActiveMenu->alias, 'something') !== false ) {
   // You are on a page where menu alias has "something" in it  
// so you can customize your template accordingly! } ?>