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

Sam*_*diy 0 php wordpress advanced-custom-fields acfpro

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

我已经尝试过了

<?php $field = get_field('field_name'); echo $field;  ?>
Run Code Online (Sandbox Code Playgroud)

<?php the_field('field_name', $post->ID); ?>
Run Code Online (Sandbox Code Playgroud)

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

<?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(); ?>
Run Code Online (Sandbox Code Playgroud)

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

小智 5

尝试使用

the_field('field_name', get_the_ID());
Run Code Online (Sandbox Code Playgroud)