【问题标题】:How can I get ACF field value in WordPress loop?如何在 WordPress 循环中获取 ACF 字段值?
【发布时间】:2019-12-08 13:19:26
【问题描述】:

我正在尝试为循环中的帖子获取 ACF 归档值。但由于某种原因,该值未显示。

我已经试过了

<?php $field = get_field('field_name'); echo $field;  ?>

还有

<?php the_field('field_name', $post->ID); ?>

这些方法都不起作用。请看下面的循环代码:

<?php 
    $args = array( 
    'post_type' => 'post',
    'posts_per_page' => 4,
    $the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <div class="col-sm-6">

        <h2 class="the-title"><?php the_field('field_name', $post->ID); ?> +  <?php the_title() ;?> </h2>


    </div>

<?php endwhile; else: ?> Nothing here <?php endif; ?>
<?php wp_reset_query(); ?>

如何在循环中获取 ACF 字段值?

【问题讨论】:

    标签: php wordpress advanced-custom-fields acfpro


    【解决方案1】:

    尝试使用

    the_field('field_name', get_the_ID());
    

    【讨论】:

      【解决方案2】:

      你没有正确关闭数组。请试试这个

      <?php 
              $args = array( 
              'post_type' => 'post',
              'posts_per_page' => 4
             );
              $the_query = new WP_Query( $args );
          ?>
          <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
              <div class="col-sm-6">
      
                  <h2 class="the-title"><?php the_field('field_name', $post->ID); ?> +  <?php the_title() ;?> </h2>
      
      
              </div>
      
          <?php endwhile; else: ?> Nothing here <?php endif; ?>
          <?php wp_reset_query(); ?>
      

      【讨论】:

        猜你喜欢
        • 2019-06-03
        • 2021-08-28
        • 2021-03-23
        • 2018-08-20
        • 2020-06-22
        • 2020-02-06
        • 2014-07-17
        • 1970-01-01
        • 2021-06-23
        相关资源
        最近更新 更多