Joomla get category ID

Unlike WordPress, Joomla has no built-function to determine ID of category or ID of article’s category. Nothing to worry though, it’s easily done. So first, getting ID of category:
(code goes in Joomla template’s file, works in all Joomla versions)

<?php
$option   = JRequest::getCmd('option');
$view   = JRequest::getCmd('view');
$temp   = JRequest::getString('id');
$temp   = explode(':', $temp);
if ($option == 'com_content' && $view == 'category' && $temp[0])
{            
$category_id  = $temp[0]; //this is category ID 
echo $category_id; //show it
}
?> 

And second, how to know in which category is current article (get ID of article’s category)? Also, not that hard:

<?php
$db = JFactory::getDBO(); 
$option   = JRequest::getCmd('option');
$view   = JRequest::getCmd('view');
$temp   = JRequest::getString('id');
$temp   = explode(':', $temp);
$id   = $temp[0];
if ($option == 'com_content' && $view == 'article' && $id)
{ $db->setQuery('SELECT cat.id FROM #__categories cat RIGHT JOIN #__content cont ON cat.id = cont.catid WHERE cont.id='.$id);   
   $category_id = $db->loadResult(); //this is current article’s category ID
echo $category_id; //show it
}?>

When you know ID of category, you can extend Joomla template to show design based on categories (i.e. specific header for each category, different CSS styles, etc). But about that in one of next articles.


Deprecated: Non-static method dc_jqslicksocial::footer() should not be called statically in /var/www/digitalette.com/wp-includes/class-wp-hook.php on line 298

Notice: Undefined index: show_post in /var/www/digitalette.com/wp-content/plugins/slick-social-share-buttons/inc/dcwp_social.php on line 805