【问题标题】:ACF field display on a page页面上的 ACF 字段显示
【发布时间】:2017-10-05 08:28:29
【问题描述】:

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

$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

我做错了什么?

【问题讨论】:

  • 当你传递它显示的其余内容时?

标签: php wordpress advanced-custom-fields


【解决方案1】:

带有单个参数的get_field() 仅适用于循环iirc 中的当前帖子,因此如果您尝试获取某个类别的数据,则必须提供一个目标。

您将需要您的类别的 termid(当您在分类页面上时,$term = get_queried_object(); $termid = $term-&gt;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/

【讨论】:

    【解决方案2】:

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

    get_field() 返回一个值。

    the_field() 回显一个值。

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

    【讨论】:

    • 我都试过了。不幸的是,它什么也没返回。在检查源代码时,我注意到它显示了我的 div 容器,但没有任何值
    • 如果将$displaytitle = 移到if(isset($menu_term-&gt;name)) 上方会怎样?
    • @JuliaGalden 尝试通过term_id 访问该值。答案已更新。
    • @JuliaGalden 试试这个:get_field('category_subtitle', 'term_' . $menu_term-&gt;term_id)
    猜你喜欢
    • 2020-07-06
    • 2019-11-17
    • 2020-11-26
    • 2019-01-26
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    相关资源
    最近更新 更多