New*_*mer 2 joomla joomla-framework joomla3.2
我需要获取当前的文章类别ID,在我使用的较旧的joomla版本中:
<?php $catid = JRequest::getInt('catid'); echo $catid; ?>
Run Code Online (Sandbox Code Playgroud)
但在Joomla 3.2中我得到0.
您可以通过利用文章模型实例被缓存的事实来消除额外的数据库查询,因此当前文章的查询结果也是如此.因此,使用内容模型类来获取您所追求的内容.
$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;
}
Run Code Online (Sandbox Code Playgroud)
注意如果您在呈现应用程序之前从系统插件调用此方法,则还必须使用require_once来包含内容模型.上述代码在大多数情况下都可以正常工作,例如模板或内容插件.