在Joomla 3.2中获取当前文章类别ID(类别) [英] Get current article category ID (catid) in Joomla 3.2

查看:139
本文介绍了在Joomla 3.2中获取当前文章类别ID(类别)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我使用的旧版joomla版本中,我需要获取当前文章类别ID:

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

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

但是在Joomla 3.2中,我得到0.

But in Joomla 3.2 I get 0.

推荐答案

您可以通过利用缓存文章模型实例的事实以及当前文章的查询结果,来消除额外的数据库查询.因此,使用内容模型类来获取您想要的东西.

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;
    }

注意,如果要在呈现应用程序之前从系统插件调用此方法,则还必须使用require_once来包含内容模型.上面的代码在大多数情况下都可以正常工作,例如模板或内容插件.

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.

这篇关于在Joomla 3.2中获取当前文章类别ID(类别)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆