简体   繁体   English

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

[英]How can I get ACF field value in WordPress loop?

I'm trying to get ACF filed value for post in loop.我正在尝试为循环中的帖子获取 ACF 归档值。 But for some reason the value is not being displayed.但由于某种原因,该值未显示。

I've already tried我已经试过了

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

And

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

None of the methods are working.这些方法都不起作用。 See the loop code below:请参阅下面的循环代码:

<?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(); ?>

How can I get the ACF field values in loop?如何在循环中获取 ACF 字段值?

Try use尝试使用

the_field('field_name', get_the_ID());

You haven't closed the array correctly.Please try this您没有正确关闭数组。请试试这个

<?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(); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM