【问题标题】:WordPress: Unable to read custom field that is part of a fields groupWordPress:无法读取属于字段组的自定义字段
【发布时间】:2021-09-22 18:07:45
【问题描述】:

我向字段组添加了一个新的自定义字段,仅用于呈现主页。当尝试使用下面的代码读取和显示该字段的内容时,没有任何反应。我确信阅读自定义字段的方式应该是这样的。我没有收到任何错误消息。你能帮忙解决这个问题吗?这是在 WordPress 网站上完成的。

<div class="bg-white-base relative pt-row pb-row pl pr">
  <div class="wrap-x">
    <div class="inside mini">
      
      <?php if ( $headline = get_sub_field( 'slider_-_h1' ) ) : ?>
      <h2 class="mb color-purple-base">
        <?php echo $headline; ?>
      </h2>
      <?php endif; ?>

      <?php if ( $subheadline = get_sub_field( 'slider_-_h2' ) ) : ?>
      <h3 class="mb alt-heading h5">
        <?php echo $subheadline; ?>
      </h3>
      <?php endif; ?>

      <?php if ( $text_area = get_sub_field( 'slider-_shortcode' ) ) : ?>
      <article class="body-area mt2x">
        <?php echo $text_area; ?>
      </article>
      <?php endif; ?>

    </div>
  </div>
</div>

【问题讨论】:

  • get_sub_field() 应该在像while( have_rows('hero') ): the_row(); ........ endwhile; advancedcustomfields.com/resources/group 这样的循环中使用,你能告诉我们吗?
  • 我认为只有当字段组具有重复字段时才需要循环。
  • 成功了,非常感谢。请将您的评论转换为答案,以便我接受。

标签: php wordpress templates customization


【解决方案1】:

get_sub_field() 应该在循环中使用。当你使用一个组时,你可以使用这样的循环:

while( have_rows('hero') ): the_row();
........
endwhile;

这里解释了一切:https://www.advancedcustomfields.com/resources/group/

【讨论】:

  • 感谢您帮我节省了很多时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多