On the page that displays a single post, I also wish to display some featured posts.
The featured posts have a meta value assigned to them to differentiate between featured and non featured posts.
The problem is that I wish to display the featured posts half way down my page but the loop starts at the top and doesn't finish until the bottom of the page.
From WP docs:
<?php
// The main query.
if (have_posts()) {
while (have_posts()) {
the_post();
the_title();
the_content();
} # End while loop
} else {
// When no posts are found, output this text.
_e( 'Sorry, no posts matched your criteria.' );
}
wp_reset_postdata();
/*
* The secondary query. Note that you can use any category name here. In our example,
* we use "example-category".
*/
$secondary_query = new WP_Query( 'category_name=example-category' );
// The second loop.
if ($secondary_query->have_posts()) {
echo '<ul>';
// While loop to add the list elements.
while ($secondary_query->have_posts()) {
$secondary_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
}
wp_reset_postdata();
?>
At the end of the first loop, you have to call wp_reset_postdata() but in my scenario, there is data that needs to be retrieved further down the page so I can't end it there.
I essentially need to do this but then only the featured posts get rendered and not the post itself.
if (have_posts()) {
while (have_posts()) {
the_post();
the_title();
the_content();
//Display featured posts half way through
$secondary_query = new WP_Query( 'category_name=example-category' );
//end featured post loop
wp_reset_postdata();
//continue outputting data from first loop
the_title();
} # End while loop.
} else {
// When no posts are found, output this text.
_e( 'Sorry, no posts matched your criteria.' );
}
//finally end inital loop
wp_reset_postdata();
Is it possible to 'pause' the loop to do a different loop then pick it back up again later on?