[wp-hackers] Pagination for custom loops

Becky Resler becky.absolute at gmail.com
Thu Feb 21 22:37:04 UTC 2013


I've done the following in several projects ...

$custom_query = new WP_Query($query_args);

if($custom_query->have_posts())
{
     // set up the pagination links
     $pagination = array(
                     'base'      => home_url().'/page-slug/%_%', //or: 
@add_query_arg("page","%#%"),
                     'format'    => 'page/%#%', // %#% is replaced by 
the actual page number
                     'total'     => $custom_query->max_num_pages,
                     'current'   => $page,
                     'show_all'  => false,
                     'end_size'  => 1,
                     'mid_size'  => 2,
                     'type'      => 'plain'
                 );

     // get the pagination links
     $pagination_links = paginate_links($pagination);

     // display the pagination links above the listing
     $html .= '<div id="pagination-top" 
class="pagination">'.$pagination_links.'</div>';

     while($custom_query->have_posts())
     {
         $custom_query->the_post();

         // put together the html for the post
     }

     // display the pagination links below the listing
     $html .= '<div id="pagination-bottom" 
class="pagination">'.$pagination_links.'</div>';
}

Becky Resler
Web Programmer
Absolute Marketing Group


On 2/21/2013 4:25 PM, Kirk Wight wrote:
> How do people handle pagination for custom loops (new WP_Query instances,
> not $wp_query)?
>
> It would be great to use the same function calls as the main loop
> (eg. get_previous_posts_link()), but these all rely on $wp_query. I've been
> hi-jacking $wp_query and resetting it with wp_reset_query() afterwards, but
> then I just feel like I've used query_posts, and become sad.
>
> Am I missing an obvious solution, or is there a better way to my own method
> short of writing all my own navigation calls?
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>



More information about the wp-hackers mailing list