
As I was creating a page template for wordpress, I wanted to list posts from a specific category on a page when I ran into a interesting problem, the query_posts function I was using was causing breaks in the execution of conditional tags like is_page(’25′) in the sidebar template. I looked around on the web for a while before finally finding the solution that worked for me. So I am rewriting the solution here for reference and so anyone else may find it easier.
Here is how you list posts from a particular category in a wordpress page template, right before the “while (have_posts()) : the_post(); ” call add:
<?php query_posts('category_name=name&showposts=100'); ?> <?php /* change name to the name of the category posts you want to display */ ?>
The reason the wordpress function query_posts() breaks the sidebar template is because its not a simple database query collecting posts from a specific category, but its resetting the query object as a whole to reflect a normal category query.
So to fix the sidebar template you have to go with creating a new query object through WP_Query() in your page template. So here is the before and after of the loop in a page template.
Page Template Loop BEFORE:
<?php query_posts('category_name=name&showposts=100'); ?> <?php while (have_posts()) : the_post(); ?> <!-- do stuff ... --> <?php endwhile; ?>
Page Template Loop AFTER:
<?php $cat_posts = new WP_Query('category_name=name'); ?> <?php if($cat_posts->have_posts()) : ?> <?php while($cat_posts->have_posts()) : $cat_posts->the_post(); ?> <!-- do stuff ... --> <?php endwhile; ?> <?php endif; ?>
Hope that helps someone, cya.














