How to retrieve section and category of an article in Joomla

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

In Joomla 1.5, you can query the database to retrieve the section and category of an article with code such as ….


// find section and category of this article
$sectionTitle = '';
$categoryTitle = '';
if ( $articleId != '' ) {
$query = 'SELECT s.title as sectiontitle, cat.title as cattitle FROM #__categories AS cat, #__content AS c, #__sections AS s WHERE c.id = ' . $articleId . ' AND c.catid = cat.id AND c.sectionid = s.id';
$db =& JFactory::getDBO();
$db->setQuery($query);
$dbResult = $db->loadAssoc();

$sectionTitle = $dbResult['sectiontitle'];
$categoryTitle = $dbResult['cattitle'];

}

For example, one can use this code in a custom plugin to display the section and category of an article in the title tag …

plugin code that retreives section and category

plugin code that retreives section and category

For more detail on how to write a custom Joomla plugin, see link.