Query Posts by Category

Fundamental 200 views
+1
0 Flares 0 Flares ×

Querying your posts by category allows you to display posts from only one or multiple categories giving you greater control over your custom page templates.

Single Category Query

<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   <?php the_content(); ?>
<?php endwhile; endif; ?>

<?php wp_reset_query(); ?>

Multiple Categories in a Query

<?php query_posts('cat=5,6,7,8'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   <?php the_content(); ?>
<?php endwhile; endif; ?>

<?php wp_reset_query(); ?>

How to find the category number?

You need to highlight your mouse over the category in question and you will see it in the bottom left hidden in the string (view screenshot below).
Finding Category Number

For Reference:

Function Reference: query_posts
Conditional Tags

Tagged with:
Posted in Wordpress

Discuss:

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

No Trackbacks.

Login

Register | Lost your password?
0 Flares Twitter 0 Facebook 0 Google+ 0 0 Flares ×