I have a big question. Whether it is possible to use a pagination for two loops. My code is,
<div class="first_div">
<?php
if (have_posts()) :
$count = 0;
while (have_posts()) : the_post();
$count++;
if ($count == 1) :
the_title();
elseif ($count == 2) :
the_title();
elseif ($count == 3) :
the_title();
endif;
endwhile;
endif;
?>
</div>
<div class="second_div">
<h3>Div between first_div and third_div</h3>
</div>
<div class="third_div">
<?php
query_posts( array('posts_per_page'=>4,'offset'=>3) );
while ( have_posts() ) : the_post();
the_title();
endwhile;
?>
</div>
From the above code, i need to display totally 7 latest news. 3 in first_div and remaining 4 in third_div. And it works great. So, now what i need to do is, i need a pagination after that third_div. But actually i need a div inbetween first_div and third_div. So i could not able to create pagination after the third_div. Whether it is possible to give a pagination
As I stated in a comment above, this can all be done in one query.
Just an important note before I jump into the thick of things, never use query_posts
Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).
( CAVEAT: This is untested, but should work )
OK, here is how we are going to do this
Run your loop as normal. We are going to use the build in loop counter ( $current_post
and remember, it starts at 0
, not 1
) to count our posts and then according to this, do something
On our first run, we are going to skip posts 4 - 7 and just display the first three in div 1
After the first run of the loop, we are going to display the pagination in div 2
To show posts 4 -7, we need to rewind our loop, and run it for the second time
On this second run, we are going to skip the first three posts, and only show post 4 - 7 in div 3
Now, let get coding
1.) Run the loop as normal and exclude/skip posts 4 - 7
if( have_posts() ) {
while( have_posts() ) {
the_post();
if( 0 === $wp_query->current_post ) {
echo '<div class="first_div">'; // Open our div container only if it is the first post
}
the_title();
if ( 2 === $wp_query->current_post ) {
echo '</div>'; // Close our div container after post three
}
if( 3 >= $wp_query->current_post ) {
//finish of the loop but displays nothing from posts 4 - 7
}
}
}
2.) Add our pagination as per normal
<div class="second_div">
<h3>Div between first_div and third_div</h3>
</div>
3.) Rewind the loop so we can rerun it
rewind_posts();
4.) Rerun the loop and only display posts 4 - 7
if( have_posts() ) {
while( have_posts() ) {
the_post();
if( 2 <= $wp_query->current_post ) {
//Skip posts 4 - 7 and displays nothing
}
if( 3 === $wp_query->current_post ) {
echo '<div class="third_div">'; // Open our div container only if it is the fourth post
}
the_title();
if ( 6 === $wp_query->current_post ) {
echo '</div>'; // Close our div container after post seven
}
}
}
That should it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.