Sunday 15 April 2012

php - Pagination for multiple loops in different div -



php - Pagination for multiple loops in different div -

i have big question. whether possible utilize pagination 2 loops. 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 , 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 above code, need display totally 7 latest news. 3 in first_div , remaining 4 in third_div. , works great. so, need is, need pagination after third_div. need div inbetween first_div , third_div. not able create pagination after third_div. whether possible give pagination

as stated in comment above, can done in 1 query.

just of import note before jump thick of things, never utilize query_posts

note: function isn't meant used plugins or themes. explained later, there better, more performant options alter main query. query_posts() overly simplistic , problematic way modify main query of page replacing new instance of query. inefficient (re-runs sql queries) , outright fail in circumstances (especially when dealing posts pagination).

(caveat: untested, should work)

ok, here how going this

run loop normal. going utilize build in loop counter ($current_post , remember, starts @ 0, not 1) count our posts , according this, something

on our first run, going skip posts 4 - 7 , display first 3 in div 1

after first run of loop, going display pagination in div 2

to show posts 4 -7, need rewind our loop, , run sec time

on sec run, going skip first 3 posts, , show post 4 - 7 in div 3

now, allow coding

1.) run loop normal , 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 if first post } the_title(); if ( 2 === $wp_query->current_post ) { echo '</div>'; // close our div container after post 3 } if( 3 >= $wp_query->current_post ) { //finish of loop displays nil posts 4 - 7 } } }

2.) add together our pagination per normal

<div class="second_div"> <h3>div between first_div , third_div</h3> </div>

3.) rewind loop can rerun it

rewind_posts();

4.) rerun loop , display posts 4 - 7

if( have_posts() ) { while( have_posts() ) { the_post(); if( 2 <= $wp_query->current_post ) { //skip posts 4 - 7 , displays nil } if( 3 === $wp_query->current_post ) { echo '<div class="third_div">'; // open our div container if 4th post } the_title(); if ( 6 === $wp_query->current_post ) { echo '</div>'; // close our div container after post 7 } } }

that should it

php wordpress pagination

No comments:

Post a Comment