michael ciccarelli is a web designer & developer out of buffalo, ny.
I am currently available for freelance and other opportunities.

Posts Tagged ‘query_posts’

Fix pagination on query_posts()

| 55 comments

article

Here is a simple solution for those of you looking to customize your WordPress theme, to use the WordPress query_posts() function to retrieve your blog posts rather than the standard loop.

I created a custom Page Template named “Blog” using query_posts() and assigned this template to be used on a designated Blog page within my site at http://michaelciccarelli.com/blog/ (a Blog within a Blog even). I did this mainly because my original homepage didn’t really resemble a traditional blog and I wanted something more than just your average archives page that only lists the titles of all your previous blog posts.

So here’s my Blog Page Template’s Source Code:


<?php
/*
Template Name: Blog page
*/
?>
<?php get_header(); ?>
<div id="content">
  <div id="primary">
    <?php
    $limit = get_option('posts_per_page');
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('showposts=' . $limit . '&paged=' . $paged .'&cat=-166,-167,-168');
    $wp_query->is_archive = true; $wp_query->is_home = false;
    ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="blog-post" id="post-<?php the_ID(); ?>">
      <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
      <div class="post-entry">
          <?php the_content(__('Read the rest of this entry &raquo;')); ?>
      </div>
    </div><!-- /post -->
    <?php endwhile; ?>

    <!-- Page Navigation -->
    <?php if (function_exists('wp_pagenavi')) : ?>
    <div class="pagenavi">
        <?php wp_pagenavi(); ?>
    </div>
    <?php else : // Use WordPress default page navigation. ?>
    <div class="pages">
        <span class="older"><?php next_posts_link('&laquo; Older Entries'); ?></span>
        <span class="newer"><?php previous_posts_link('Newer Entries &raquo;'); ?></span>
    </div>
    <?php endif; ?>
    <?php else : ?>
    <div class="blog-post">
        <h2 class="post-title"><?php _e('404 - Not Found'); ?></h2>
        <div class="post-entry">
          <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
          </div>
  </div>
  <?php endif; ?>
      </div>
      <?php get_sidebar(); ?>
</div><!-- content -->
<span class="clr"></span>
<?php get_footer(); ?>

Notes:

The only bit you may want to alter in the function that retrieves your posts is the 3rd line in from top:query_posts('showposts=' . $limit . '&paged=' . $paged .'&cat=-166,-167,-168');

Notice the &cat=-166,-167,-168 this basically says exclude the following category IDs, which in my case are portfolio categories, that I don’t want to display in my blog section. This parameter is more commonly used like cat=1,3,5 which will only return posts from those categories.

$limit is assigned the ‘posts_per_page’ option from your WordPress settings, this stopped working for me for some reason but not a big deal because it can be changed to something specific, like: $limit = 20;


$wp_query->is_archive = true;
$wp_query->is_home = false;

These variables that come after the query_posts() are important as they force posts_nav_link() (and so pagination) to work, along with a few other helpful results gained for fooling WordPress into thinking we’re in the archive pages.

For the $paged stuff, see: this support forum postSource [via WordPress Support]