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 »')); ?>
</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('« Older Entries'); ?></span>
<span class="newer"><?php previous_posts_link('Newer Entries »'); ?></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 post – Source [via WordPress Support]
Tags: pagination, query_posts, WordPress
@daniel…shoot me an email I’ll take a look for you…anyone else having problems I’m much more responsive via email…sorry for the delay getting back to you(s)
Mike
mike@badapplemedia.com
Very nice just solved my problem ROCK ON!
Mike – thanks so much for this post. I created my first new template for WordPress and couldn’t figure out why pagination wasn’t happening. I was able to get it working immediately after reading your post.
You rock!
you RAWKS
Thanks, this saved me a lot of time.
Thank you very much!
The only “bug” I have found is that it displays the whole post, not only until the , also if there is “”. How can we solve it?
Thanks
[...] Fix pagination on query_posts(), MikeChick.net [...]
[...] Fix pagination on query_posts(), MikeChick.net Aggiungi un avatar alla tua firma (gratuito e valido per tutti i blog): http://en.gravatar.com/ [...]
Thank u guy,
nice article, you helped me so much with this information!
Thank you so much! I was tearing my hair out trying to figure out how to get pagination and still separate my posts by categories. This totally saved my sanity.
[...] a look at this: http://michaelciccarelli.com/2008/07/fix…n-query-posts/ Make sure you run get_query_var() before ANY query_posts on the page though. heres a sniplet of [...]
[...] As a very useful way of excluding a category from the index. But it doesn’t work so well on archives. Especially not when these are paginated. The problem was that now every older or newer entry was actually the same. Thankfully, the brilliant Michael Ciccarelli, figured out a way to overcome this problem: [...]
[...] original post here: Fix pagination on query_posts() | MikeChick.net Tags: appid – detected – invalid – missing Comments0 Leave a Reply Click here to cancel [...]
Thanks for this! Now I don’t have to be forced to list all my posts in one big long page.
Thank You!
Thank you thank you thank you! You saved my butt with that.
thank you so much. this helped a lot.
Thank you so much!!!
THANK YOU! Wish I found this 3 hours ago lol
Immensely helpful. Many thanks for this straightforward and well-written explanation.
Man you rock. I was getting very frustrated with this pagination mess. thanks a bunch for the enlightenment.