Theme Joomla Tips and Advice

Useful PHP conditional statements for Joomla

While doing some coding for one of our new templates I was looking for a clearer understanding of some of the conditional statements to be used with the latest versions of Joomla, as I am well versed in the older methods. I found some, and thought I would list them below should anyone find them useful...

//Write conditional code for a homepage
<?php if (JRequest::getVar('view')=='frontpage') { ?>
put the HTML for the banner here
<?php } ?>
 
//Write conditional code for a Section Page
 <?php if (JRequest::getVar('view')=='section') { ?>
put the HTML for the banner here
<?php } ?>
 
//Target a Section page with a particlar #id
<?php if (JRequest::getVar('view')=='section' && JRequest::getVar('id')==1) { ?>
put the HTML for the banner here
<?php } ?>
 
//If on section 1, display this data, if on section 2, display this data
<?php
$db = &JFactory::getDBO();
$id = JRequest::getVar('id');
Useful
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 }
?>

These above are great for sections and categories, if I find more for other sections of Joomla, I will put them online. Enjoy and happy coding!

members

search