[英]Add custom field from plugin to joomla article loop
I created 3 custom fields for my joomla com_content articles like described in this tutorial: https://docs.joomla.org/Adding_custom_fields_to_core_components_using_a_plugin/de 我为joomla com_content文章创建了3个自定义字段,如本教程中所述: https : //docs.joomla.org/Adding_custom_fields_to_core_components_using_a_plugin/de
Now i need to create an overview over all articles inside a category (43) and show these custom fields inside a joomla query. 现在,我需要为类别(43)中的所有文章创建概述,并在joomla查询中显示这些自定义字段。
My actual joomla query inside the template file override for articles: 我在模板文件中实际的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 />';
}
?>
Custom fields can be added to an article output with: 自定义字段可以通过以下方式添加到文章输出中:
$this->params->get('custom_field_1');
But this is not working inside the loop. 但这在循环内不起作用。 How can I add a custom field with the name
custom_field_1
to this loop? 如何在此循环中添加名称为
custom_field_1
的自定义字段?
You should use method onContentPrepare of a content plugin, the same you used to add field in form : 您应该使用内容插件的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();
}
Now in your item you've got a field articles with the list of all articles of category 43. 现在,在您的项目中,您将获得一份包含所有类别为43的文章列表的领域文章。
In your view, you'll be able to use $this->item->articles to retrieve this list. 在您看来,您将可以使用$ this-> item-> articles检索此列表。
You can try below code within loop- 您可以在循环内尝试以下代码-
<?php
$attribs = json_decode($article->attribs);
echo $attribs->custom_field_1;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.