Simple Post Pagination

WordPress has pretty solid build-in pagination, but figuring out how to use it might sometimes be a challenge. This recipe shows how to implement basic pagination with Previous & Next links.

Getting the actual previous/next links is pretty straight forward. Simply use the following two functions:

previous_posts_link("Previous Label");
next_posts_link("Next Label");

Things to note here; These functions output (echo) an a tag and they don’t have optional parameters to specify style or other attributes. A bit of an oversight if you ask me. You can use these functions on any archive or other template that displays a list of posts.

However, sometimes you have a page template on which you run a custom query. Luckily pagination works there as well. Just run your custom query and use the above functions to generate the links:

global $wp_query;

query_posts(
   array(
      'paged' => $wp_query->query_vars["paged"],
   )
);

Easy peasy… Bon App├ętit!

Tagged with: