Behind the veil of Colorado's premier Interactive Agency

Wordpress – Custom Paging Jasper Showers

July 11th, 2008

I recently ran into a bit of a pickle while trying to make the load time on a category page faster in Wordpress. The main problem I was facing was an issue with the paging. The home page needed to display 10 posts where the slow-loading category page only needed to display 3 posts. Normally something like this is easily implemented with a category template and the query_posts function, but because query_posts oftentimes has unexpected results, it wasn’t an option.

I began to google for a solution and ran across this page that helped me solve my problem. By making a custom loop in a theme template, I was able to implement the functionality of query_posts, using WP_Query. This turned out to be an easy fix and solved all of my paging woes.

I replaced the standard Wordpress loop:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
…………..content goes here
<?php endwhile; endif; ?>

With my own custom loop:

<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query(’orderby=date&order=DESC’.'&cat=1′.’&showposts=3′.’&paged=’.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
…………..content goes here
<?php endwhile; ?>
<?php $wp_query = null; $wp_query = $temp;?>

The $wp_query->query() function is where the magic happens. Any parameter you could pass to query_posts can be placed here to control what posts will be shown on the page. It will also allow for custom paging which is what I needed. If you are having problems with query_posts, I suggest giving this method a try. All loop variables and functions are still available to you, and it won’t mess up plugin functionality.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Reddit
  • Slashdot
  • del.icio.us
  • StumbleUpon
  • Netvouz
  • ThisNext

Post a Comment