![](/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.