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 badge1 Answer
Reset to default 0I'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条)