Finding the Section Alias (or Title) in Joomla

  • PDF

The following is a nice little code segment for finding the section alias and using it to set the Body ID. This allows us to vary the styling based on the Body ID tag.

<?php 
$db =& JFactory::getDBO();
function getSection($iId) {   
$database = &JFactory::getDBO();

$sql = "SELECT s.alias ";    
$sql .= "FROM #__sections as s join #__content as c on c.sectionid = s.id ";   
$sql .= "WHERE c.id = $iId ";
//debug echo $sql;
$database->setQuery( $sql );
$row=$database->loadResult();
return $row;
}
$section=getSection(JRequest::getInt( 'id'));
if $section == "" $section = "home";
?>
< body id="<?php print $section; ?>">

Instead of using alias you could change the first part of the select statement to pull in the title:

$sql = "SELECT s.title ";

However, remember that Titles can have spaces and CSS does not like it when a class id has spaces.