ACF字段显示在页面上

ACF field display on a page

我有问题。我通过 ACF wordpress 插件创建了一个自定义字段。它是自定义 post 类型类别的字段(假设它是对该类别的附加描述)。我试图通过这样的代码将它添加到我的页面:

$return_html.= '<h2 class="ppb_menu_title" ';
$return_html.= '>'.$menu_term->name.'</h2><br class="clear"/><br/>';
$displaytitle = the_field('category_subtitle');
$return_html.= '<div class="subtitledesc">'.$displaytitle.'</div>';

下面的代码是整页代码的一部分,您可以在此处找到 [第 1712-1715 行]: https://codeshare.io/50QzqL

我做错了什么?

您需要使用 get_field() 而不是 the_field() 并包含术语 ID。

get_field() returns 一个值。

the_field() 回显一个值。

试试这个:get_field('category_subtitle', 'term_' . $menu_term->term_id)

带有单个参数的

get_field() 仅适用于循环 iirc 中的当前 post,因此如果您尝试获取某个类别的数据,则必须提供目标.

您需要类别的术语(当您在分类页面上时,$term = get_queried_object(); $termid = $term->term_id; 应该可以),然后像这样使用 get_field:

get_field( 'category_subtitle', "taxonomyname_$termid" );
get_field( 'category_subtitle', $term ); // if you have a term object

进一步阅读:https://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/