Get current article category ID (catid) in Joomla 3.2

我们两清 提交于 2019-12-24 00:42:51

问题


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.


回答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.




回答2:


Try this

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

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




回答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');



回答4:


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



来源:https://stackoverflow.com/questions/22877545/get-current-article-category-id-catid-in-joomla-3-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!