Get current article category ID (catid) in Joomla 3.2

Newcomer picture Newcomer · Apr 5, 2014 · Viewed 12.6k times · Source

I need to get current article category id, in older joomla version I used:

<?php $catid = JRequest::getInt('catid'); echo $catid; ?>

But in Joomla 3.2 I get 0.

Answer

David picture David · Jun 9, 2015

You can eliminate the extra database query by taking advantage of the fact that the article model instance is cached and so is the query result for the current article. So, use the content model class to get what you are after.

    $app = Jfactory::getApplication();
    $input=$app->input;
    if ($input->getCmd('option')=='com_content' 
    && $input->getCmd('view')=='article' ){
        $cmodel = JModelLegacy::getInstance('Article', 'ContentModel');
        $catid = $cmodel->getItem($app->input->get('id'))->catid;
    }

NB if you are calling this from a system plugin before the application is rendered you will have to also have to use require_once to include the content model. The above code will work fine in in most situations such as a template or content plugin.