Get current article category ID (catid) in Joomla 3.2

13,068

Solution 1

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.

Solution 2

Try this

<?php echo $this->item->catid;?>

This work in blog_item.php the category folder, and blog.php of article folder.

Solution 3

I know this is an old post but, it helped me figure out just what I needed.

To get the catid, view, and layout:

$a = JFactory::getApplication();
 $input=$a->input;

 $catId = $input->getCmd('id');
 $view  = $input->getCmd('view');
 $layout = $input->getCmd('layout');
Share:
13,068
Newcomer
Author by

Newcomer

Updated on June 17, 2022

Comments

  • Newcomer
    Newcomer about 2 years

    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.

  • Metagrapher
    Metagrapher almost 3 years
    I think I might be following this answer, but it seems to be missing some details. I think the suggestion to check the URL may be a good one... Perhaps you meant to include some markdown but it didn't work out? Try editing your answer to see if you can add some clarity?
  • mickmackusa
    mickmackusa almost 3 years
    @MetagrapherMeta I have fixed the formatting problem.
  • mickmackusa
    mickmackusa almost 3 years
    @Thierry if you are a Joomla user, please join us at Joomla Stack Exchange.