wp query - ACF: using two loops, the_field returns field content from another loop

Essentially on my single.php file, I'm querying the current post directly into the template, while having also havi

Essentially on my single.php file, I'm querying the current post directly into the template, while having also having a related posts section.

The issue is that when ACF tries to retrieve the field of the post inside the related post query loop, it retrieves the current displayed post instead.

while(have_posts()){ the_post();
    echo the_field('field1');
    echo the_field('field2');
    echo the_field('field3');
}

$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ){ ?>
    <img src="<?php echo the_field('field1')?>">
<?php
    echo the_title();
 }

So essentially it grabs field1 from the current post instead of the recent_posts query. I've been very confused about this issue. the loop and query are out of the scope of the while loop, so it should be fine right?

Essentially on my single.php file, I'm querying the current post directly into the template, while having also having a related posts section.

The issue is that when ACF tries to retrieve the field of the post inside the related post query loop, it retrieves the current displayed post instead.

while(have_posts()){ the_post();
    echo the_field('field1');
    echo the_field('field2');
    echo the_field('field3');
}

$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ){ ?>
    <img src="<?php echo the_field('field1')?>">
<?php
    echo the_title();
 }

So essentially it grabs field1 from the current post instead of the recent_posts query. I've been very confused about this issue. the loop and query are out of the scope of the while loop, so it should be fine right?

Share Improve this question asked Nov 10, 2017 at 16:50 Chris FamChris Fam 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

I'm not fun of helper functions so I would write it like this:

$args=array(
    'post_type' => 'post',
    'posts_per_page' => '20',
    'post_status' => 'publish',
    'order'=>'DESC',
    'orderby'=>'ID',   
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();

        $current_id = $post->ID;
       echo the_field('field1', $current_id);

    }
    wp_reset_postdata();
} else {
    // no post
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745306447a4621739.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信