最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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
}
发布评论

评论列表(0)

  1. 暂无评论