Fix pagination on query_posts()
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 »')); ?>
<?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('« Older Entries'); ?></span>
<span class="newer"><?php previous_posts_link('Newer Entries »'); ?></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:
query_posts('showposts=' . $limit . '&paged=' . $paged .'&cat=-166,-167,-168');$limit = 20;$wp_query->is_archive = true;
$wp_query->is_home = false;
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]











thanks a lot, you’ve saved my day
Glad to help.
Hi Michael,
just wanted to let you know that i love your page and think you’re extemely handsome;)
Staci
http://wordpress.org/support/topic/152451?replies=12#post-679318
Thanks Wilo. I’ve updated the post to credit this source, which was actually where I came up with query being used in my blog template.
So glad to see that this solution just came out so maybe I’m not that far behind.
Thanks so much!