2

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.

Michel
  • 490
  • 4
  • 11
  • You need to get it in what context? For example in the layout? Or in the model? Or a plugin or helper? – Elin Apr 05 '14 at 14:06

5 Answers5

8

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.

David
  • 1,179
  • 12
  • 15
1

Try this

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

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

Leoalv
  • 25
  • 8
1

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');
Figureo Sfm
  • 31
  • 1
  • 3
0

It will depend on your context. In joomla 3 the category id is not in the www-query when you display an article. So you need to get it from the article item. To investigate if catid is available in some of your variables, you could try to do:

<?php print_r($this); ?>

or

<?php print_r(get_defined_vars()); ?>

To get the catid directly in the template, if it's not available in the output above, you might do something like this:

$input=Jfactory::getApplication()->input;
if($input->getCmd('option')=='com_content' 
&& $input->getCmd('view')=='article' ){
  $db=JFactory::getDbo(); 
  $db->setQuery('select catid from #__content where id='.$input->getInt('id')); 
  $catid=$db->loadResult(); 
}

(this will generate some extra database-traffic) regards Jonas

jonasfh
  • 4,151
  • 2
  • 21
  • 37
0

You have to look carefully at the url.

Instead of:

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

it could be:

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

Below is an example URL I have:

index.php?option=com_content&view=category&layout=blog&id=8&Itemid=103

You see the category id can be found in id=8

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 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? – Metagrapher Jun 28 '21 at 03:25
  • @MetagrapherMeta I have fixed the formatting problem. – mickmackusa Jun 28 '21 at 07:48
  • @Thierry if you are a Joomla user, please join us at [joomla.se] Stack Exchange. – mickmackusa Jun 28 '21 at 07:49