繁体   English   中英

将文章加载到Joomla中的组件模板中

[英]Loading an article into a components template in Joomla

我想将文章加载到Joomla框架内的组件模板php代码中。

我可以在php中加载模块,在文章中加载模块,在文章中添加组件,并且..但我从来不想将文章加载到组件php中。

有人知道为此的代码段吗?

感谢任何帮助。

我会在您的视图中加载文章模型,例如

JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models', 'ContentModel');
$model = JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request' => true));
$article = $model->getItem((int) $articleId);

现在,您可以访问文章中所有可用的字段,例如$item->fulltext$item->introtext 在显示文章之前,请查看文章视图以检查它对文章所做的所有奇特的东西。

使用Joomla .3.8.10时出现Fatal error: __clone method called on non-object in .../components/com_content/models/article.php on line 164

似乎在模型中需要params-property:

use Joomla\Registry\Registry; // only for new Registry below
ModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models', 'ContentModel');
$model=JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request'=>true));
// $params=JFactory::getApplication()->getParams();
// An empty registry object is just fine:
$params=new Registry;
$model->setState('params', $params); // params (even empty) is *required* for model
$article=$model->getItem((int) articleId);

不确定是否需要ModelLegacy::addIncludePath...或在什么情况下需要。 有人对此有见识吗?

更新资料

您可能要使用带有id过滤器的Article 模型(请注意复数),因为它还可以获取标签和关联:

use Joomla\Registry\Registry; // for new Registry
$model=JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request'=>true));
$model->setState('params', new Registry);
$model->setState('filter.article_id', (int) $articleId ); // or use array of ints for multiple articles
$model->setState('load_tags', true); // not available for Article model
$model->setState('show_associations', true);
$articles=$model->getItems();
$article=$articles[0];

一些更多的过滤器

如果您想通过id以外的其他方式获取文章(默认为默认)。 components/com_content/models/articles.php

  • filter.article_id:单个整数或整数数组
  • filter.article_id.include:(bool)true | false true包含false以排除给定的id
  • -> dito类别,作者,author_alias
  • filter.subcategories:(布尔型)false | true
  • filter.max_category_levels:(int)1
  • filter.date_filtering :(字符串)偏离|范围|相对
  • filter.date_field :(字符串)创建的数据库表字段 ²
  • filter.start_date_range 用于filter.date_filtering ==范围
  • filter.end_date_range 用于filter.date_filtering ==范围
  • filter.relative_date(int)0 从今天开始过去的天数,对于filter.date_filtering == relative
  • filter.tag(int)0 标签ID
  • load_tags :(布尔)true | false
  • show_associations :(布尔)false | true

订货与限制

  • list.ordering :(字符串)对数据库表字段的排序²
  • list.direction :(字符串)ASC | DESC
  • list.limit:(int)
  • list.start:(int)0 从零开始

)²有效字段:id,标题,别名,checked_out,checked_out_time,catid,category_title,状态,访问权限,access_level,创建,created_by,排序,功能,语言,匹配次数,publish_up,publish_down,图像,URL,filter_tag。 所有字段均已根据白名单进行检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM