Multiple loop with independent pagination and permalinks
-
Hi everybody,
i tried to find something about Multiple loop with independent pagination in the forums and nothing; please, you say me if i am wrong.
I found this and this but both topics are not resolved. Nothing either searching google.
After some work I found a solution by myself. Different pagination for each loop in a two loops page works fine now, but not to implement permalinks in them.
So my code produces two numerical pagination system using WordPress paginate_links() native function. For the first loop, prepared with query_posts to get posts from cat 1, the links are like this:
http://example.com/?cat=1&paged=2
For the second one, prepared to list posts from cat 2, like this:
http://example.com/?cat=2&paged=2
So, the navigation between pages works fine. You can see here, in Projects and Blog tabs.
The loops code:
<?php /* begin first loop */ $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $query_cat = 1; query_posts("posts_per_page=2&cat=$query_cat&paged=$paged"); if ( have_posts() ) : while ( have_posts() ) : the_post(); /* first loop HTML code */ endwhile; include "pagination.php"; endif; wp_reset_query(); /* end first loop */ /* begin second loop */ $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $query_cat = 2; query_posts("posts_per_page=5&order=ASC&cat=$query_cat&paged=$paged"); if ( have_posts() ) : while ( have_posts() ) : the_post(); /* second loop HTML code */ endwhile; include "pagination.php"; endif; wp_reset_query(); /* end second loop */ ?>
And the content of the pagination.php file:
<?php $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1; $current_cat = $query_cat; $pagination = array( 'base' => @add_query_arg( array('cat' => $current_cat, 'paged' => '%#%'), $_SERVER['SERVER_NAME'] ), 'format' => '', 'total' => $wp_query->max_num_pages, 'current' => $current, 'show_all' => false, 'prev_text' => __('«'), 'next_text' => __('»'), 'type' => 'plain', ); echo paginate_links($pagination); ?>
All this code works fine, but if i add the following code to implement permalinks, then brokes:
global $wp_rewrite; if( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg('s',get_pagenum_link(1) ) ) . "page/%#%/", 'paged');
I have also tried to change the las line into the following with no results:
$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg('s',$current_uri ) ) . "$current_cat_name/", 'cat' . "page/%#%/", 'paged');
Any idea?
- The topic ‘Multiple loop with independent pagination and permalinks’ is closed to new replies.