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
Thank you SO much!
[...] link: Fix pagination on query_posts() / scriptflipper.com Tags: [...]
hi,
thanks a lot. i tried this code, its very useful.
1st of all i am not web developer or have not taken any training. I just do it by reading instruction.
my problem is this code show full posts by default (at least for my site. I want it show read more tag after few lines of my post.
Great and useful, but I am having a problem. This code works great as follows with pagination working correctly:
query_posts(‘showposts=’ . $limit . ‘&paged=’ . $paged . ‘&cat=’ . 14);
However, when I change the &cat to a variable instead of a static the pagination breaks. I think this is because the query generates ALL records and not just records in cat 14.
Any suggestion about why this might be or how to correct it? Thanks so much.
Followup: The problem seems to be one of variable visibility. I am passing the category variable freom a form using:
$beds = $_POST['beds'];
… where the value of “beds” is “14″. If I assign the $beds = 14 directly in my search query file then pagination works as intended, but if I simply assign $beds via $_POST the variable seems to have lost visibility. Any ideas?
Thanks.
Really appreciate your post. I was able to use a piece of that code to fix my problem. Yay for query pagination!
Very very good sir, thank you so much for this! I have been looking for about 3 days for this.
Hi, Michael -
No luck for me with this one. The pagenavi shows up, but clicking the numbers doesn’t advance the pages at all. The pages are numbered in the browser’s address bar, but the query still returns the same data. My code as follows:
<a href="” rel=”bookmark” title=”Permanent Link to “>
ID)) {
echo “”.userphoto($curauth).”";
} else {
echo “”.get_avatar($curauth->ID, 64).”";
}?>
by
Chk email Dwaynne
That’s strange. It’s not working for me. I am using wordpress 3.0.1.
Like Dwaynne, navigation shows up but it stays on the 1st page.
My code as follows.
$paged=(get_query_var(‘paged’))?get_query_var(‘paged’):1;
query_posts(“cat=4&paged=$paged”);
This got me out of a fix that had me for hours, just wanted to say thank you very much.
too too too too many thanks!!! too much helpfull!!!!
Thank you! Thank you! Thank you!
Thanks a ton.. Exactly What I needed..
You rock! Thanks!