【问题标题】:Using get_field with specific postID returns empty使用带有特定 postID 的 get_field 返回空
【发布时间】:2019-06-11 06:44:34
【问题描述】:

我正在尝试在循环中使用 get_field 来检索一些自定义字段值,但是 使用 get_field('container', post_id) 时,该值始终为空。

我什至尝试在块文件中使用它,但发生了同样的情况。

  $container = get_field('container'); // this works
  $test_container = get_field('container', 144); //this returns empty,  post id === 144 

【问题讨论】:

  • 使用get_post(144) 测试它是否可以为您检索帖子数据
  • get_post(144) 返回帖子数据

标签: php wordpress advanced-custom-fields


【解决方案1】:

你有没有尝试过_field()函数。

在 ACF 中,the_field() 函数用于显示特定字段的值。它与 get_field() 相同。 https://www.advancedcustomfields.com/resources/the_field/

<?php if( get_field('container') ): ?>
   <p><?php the_field('container', 144); ?> </p>
<?php endif; ?>

【讨论】:

    【解决方案2】:

    您使用的代码应该可以工作,但如果由于任何原因无法正常工作。你也可以使用

    get_post_meta(); //to retrieve the value.
    

    【讨论】:

    • 唯一返回的是[_edit_lock] =&gt; Array ( [0] =&gt; 1560233544:1 )
    猜你喜欢
    • 2017-05-28
    • 2019-11-03
    • 2014-09-13
    • 2017-08-22
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    相关资源
    最近更新 更多