![](/img/trans.png)
[英]Custom Field of plugin does not update the value when assigned in Joomla Article
[英]Add custom field from plugin to joomla article loop
我为joomla com_content文章创建了3个自定义字段,如本教程中所述: https : //docs.joomla.org/Adding_custom_fields_to_core_components_using_a_plugin/de
现在,我需要为类别(43)中的所有文章创建概述,并在joomla查询中显示这些自定义字段。
我在模板文件中实际的joomla查询覆盖了文章:
<?php
$catId = 43;
$query = "SELECT * FROM #__content WHERE catid ='" . $catId . "'";
$db = JFactory::getDBO();
$db->setQuery($query);
$articles = $db->loadObjectList();
foreach($articles as $article){
echo 'ID: ' . $article->id;
echo '<br />';
echo 'Name: ' . $article->title;
echo '<br />';
echo '<a href="' . JRoute::_('index.php?option=com_content&view=article&id='.$article->id) . '">Link</a>';
echo '<br /><br />';
}
?>
自定义字段可以通过以下方式添加到文章输出中:
$this->params->get('custom_field_1');
但这在循环内不起作用。 如何在此循环中添加名称为custom_field_1
的自定义字段?
您应该使用内容插件的onContentPrepare方法,就像在form中添加字段一样:
public function onContentPrepare($context, &$row, $params, $page = 0){
if ( JFactory::getApplication()->getTemplate() !== 'your_template_name' ){
return;
}
$catId = 43;
$query = "SELECT * FROM #__content WHERE catid ='" . $catId . "'";
$db = JFactory::getDBO();
$db->setQuery($query);
$row->articles = $db->loadObjectList();
}
现在,在您的项目中,您将获得一份包含所有类别为43的文章列表的领域文章。
在您看来,您将可以使用$ this-> item-> articles检索此列表。
您可以在循环内尝试以下代码-
<?php
$attribs = json_decode($article->attribs);
echo $attribs->custom_field_1;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.