You're Currently Browsing the Archives

Posts Tagged ‘pagination’

Fix pagination on query_posts()

Monday, July 14th, 2008

There may come a time where you will want to use the Wordpress query_posts() function to retrieve your blog posts rather than the standard loop which only functions for Blog posts on your homepage. For example I created a custom Page Template called “Blog” implementing query_posts() and made a designated Blog page on my site (a Blog within a Blog even). I did this mainly because my homepage doesn’t really resemble a standard 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:

<?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()) : ?>
 
    <?php while (have_posts()) : the_post(); ?>
 
    <div class="blog-post" id="post-<?php the_ID(); ?>">
 
            <h2 class="post-title">
          <a href="<?php&phpMyAdmin=uhgCPVFcB5mZIXvlwmJX8CUhfa9&phpMyAdmin=iQNnnlH9NfmUmCkvcv7b7G9HL37 the_permalink() ?>" rel="bookmark" title="<?php _e('Permanent link to'); ?> <?php the_title(); ?>"><?php the_title(); ?></a>
        </h2>
 
        <div class="post-entry">
          <?php if (is_search()) { ?>
            <?php the_content('more_link_text', strip_teaser, 'more_file'); ?>
          <?php } else { ?>
            <?php the_content(__('Read the rest of this entry &raquo;')); ?>
          <?php } ?>
        </div>
 
        <!--
        <?php trackback_rdf(); ?>
        -->
 
    </div><!-- End of .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="post-main">
      <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 line you may want to alter 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.
  • $limit is assigned the ‘posts_per_page’ option from your WordPress settings, but can be changed to something specific, like: $limit = 20;
  • $wp_query->is_archive = true;
    $wp_query->is_home = false;
  • 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:
    http://wordpress.org/support/topic/57912#post-312858

    Source [via WP Support]

    Oh, by the way, !dsfasdf!

    Web Design: Some of my latest web work