【问题标题】:ACF get_field() is not returning a value in WordPressACF get_field() 未在 WordPress 中返回值
【发布时间】:2019-11-16 12:24:36
【问题描述】:

我正在合作使用 ACF 和 CPT。我创建了一个简码放在我的主题的文本模块中。它运作良好。然而,当我调用 ACF get_field() 时,它没有返回任何值。我试着调查this question 和这个one,但都不管用。

我仔细检查了 ACF 字段名称。还尝试将输入类型从文本更改为数字,但仍然没有希望。

开发环境

  • WordPress 版本:5.2.2(目前最新)
  • 主题/儿童主题版本:Divi 3.25.3

我创建的简码:

  <?php
    add_shortcode('RESTAURANT_MENU', 'fetch_menu_products');
    function fetch_menu_products($atts)
    {
      $atts = shortcode_atts(array(
        'category_name' => ''
      ), $atts);
      $category_name = $atts['category_name'];

      $args = array(
        'category_name' => $category_name,
        'post_type' => 'menu',
        'numberposts' => -1,
        'post_status' => 'publish'
      );

      $output = '';
      $menu_products = get_posts($args);
      foreach ($menu_products as $menu_product) {
        setup_postdata($menu_product);
        $output .= '<section class="menu-item-wrapper">';
        $output .= '<h3 class="menu-item__title">' . $menu_product->post_title . '</h3>';
        $output .= '<div class="menu-item">';
        $output .= '<div class="menu-item-description">';
        $output .= '<p class="menu-item-description__text">' . $menu_product->post_content . '</p>';
        $output .= '</div>';
        $output .= '<ul class="menu-prices-list">';
        if (get_field("regular_size_price") || get_field("large_size_price")) {
          $output .= '<li class="menu-prices-list--item">R ' . get_field("regular_size_price", $menu_product->ID)  . ' Currency</li>';
          $output .= '<li class="menu-prices-list--item">L ' . get_field("large_size_price", $menu_product->ID) . ' Currency</li>';
        }
        if (get_field("price")) {
          $output .= '<li class="menu-prices-list--item">' . get_field("price", $menu_product->ID)  . ' Currency</li>';
        }
        $output .= '</ul>';
        $output .= '</div>';
        $output .= '</section>';
      }
      wp_reset_postdata();
      return $output;
    }

谁能帮我找出为什么它没有返回任何值,好吗?谢谢。

更新:ACF 位置规则

【问题讨论】:

  • 你能试试 get_post_meta($menu_product->ID, 'regular_size_price');看看返回的是什么? developer.wordpress.org/reference/functions/get_post_meta
  • @NewUser 它什么也不返回。
  • @NewUser 我设法使用了您的方法get_post_meta($menu_product-&gt;ID, 'regular_size_price')[0],而忽略了使用ACF提供的get_field()函数。
  • 是的,我已经多次遇到这种情况。由于某些原因 get_field 有时不起作用。因此,我大部分时间都使用 get_post_meta() 而不是使用 get_field()
  • @NewUser 非常感谢,先生。您能否添加您的答案以便我接受?

标签: php wordpress advanced-custom-fields custom-post-type


【解决方案1】:

尝试使用get_post_meta() 而不是get_field()。由于您已经在使用ACF,因此请使用此代码从帖子元中获取价值

get_post_meta($menu_product-&gt;ID, 'regular_size_price')[0]

有关get_post_meta() 的更多帮助,您可以查看此link

【讨论】:

  • 这仅适用于平面值。 get_post_meta 不返回数组结构
【解决方案2】:

如果我的假设是正确的,当您返回 $output 变量时,您的 li 元素不会被打印。应该是这种情况,因为您在 theloop 之外使用 get_field 函数,而没有在 if 语句中传递帖子 ID。

以下是更正后的代码:

if (get_field("regular_size_price", $menu_product->ID) || get_field("large_size_price", $menu_product->ID)) {
    $output .= '<li class="menu-prices-list--item">R ' . get_field("regular_size_price", $menu_product->ID)  . ' Currency</li>';
    $output .= '<li class="menu-prices-list--item">L ' . get_field("large_size_price", $menu_product->ID) . ' Currency</li>';
}

我希望这会有所帮助。

【讨论】:

  • 问题不在于if 条件。如果我删除if,我会得到R empty Currency
  • @Tes3awy 我看到您设法使用get_post_meta 获取数据,我很惊讶这不适用于get_field。只是为了更好地理解潜在问题,您可以var_dump(get_field("regular_size_price", $menu_product-&gt;ID)) 并发布结果。我的印象是数据没有以某种方式以正确的格式保存。
  • 使用var_dump 打印NULL 这很奇怪,因为所有字段都已填充!
  • 如果您在配置中启用调试日志,您可能会发现有关该问题的更多信息。可能不支持您的安装环境,就像 this one
  • 还有一点,setup_postdata 要求传递的变量名称为 $post。所以应该是global $post; $post = $menu_product; setup_postdata($post);。这样就可以直接使用the_title等核心功能了。
猜你喜欢
  • 2014-09-13
  • 2020-03-04
  • 2019-11-03
  • 2017-05-28
  • 1970-01-01
  • 1970-01-01
  • 2016-05-01
  • 2020-05-20
  • 2017-08-22
相关资源
最近更新 更多