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

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

问题描述

我需要在我使用的旧 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 (catid)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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