Grow your CSS skills. Land your dream job.

Last updated on:

Run Loop on Posts of Specific Category

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

If you were to use this, for example, in a left sidebar which ran before the main loop on your page, remember to reset the query or it will upset that main loop.

<?php wp_reset_query(); ?>

Comments

  1. Jake
    Permalink to comment#

    Is there a way to display a certain number of items and then have a link at the bottom for older posts?

    • Webweaver
      Permalink to comment#

      Yes to question1. I have imported the most recent posts of a category into my website by using query_posts('category_name=whatevernameyouuse &posts_per_page=6');

      I have not looked at linking just to older posts, I just linked to my blog but I bet there is a way to do it…

    • Webweaver
      Permalink to comment#

      Or for a more authoritative look at how to do recent posts, look on this website for the recent posts article. http://css-tricks.com/snippets/wordpress/recent-posts-function/

  2. Permalink to comment#

    veru nice..
    thanks for sharing..
    i need this tuts for my web project..

  3. I want to create a loop that contains only posts that are in two specified categories – I just don’t get how to do this. Is anyone here to help me out of this problem?

    • ubrayj02
      Permalink to comment#

      See where it says cat=5?

      <?php query_posts('cat=5'); ?>

      Make it say cat=5,6,7

      Then, you will have fetched posts from categories 5, 6, and 7.

      If you want to limit the number of posts, stick a “showposts” in there to make something like this:

      <?php query_posts("showposts=2&cat=13,14"); ?>

  4. Brian
    Permalink to comment#

    This is awesome. Where are the valid places I can use this snippet of code? I want to display a custom loop on a page.

  5. Ferik
    Permalink to comment#

    where I have to placephp wp_reset_query();?

    • Permalink to comment#

      I personally place it after the ?php endwhile; endif; ?> I don’t know if it is a good practice but seen to work fine for me!

  6. Ben Yates
    Permalink to comment#

    Hi,
    Your post was a great help.

    The only difficulty I am having is setting the category to show from an id entered into a custom field when writing the post.

    I’ve tried PHP echo and grabbing the field using a plugin but still struggling to set the category based on what the custom field value is.

    Any help would be greatly appreciated.

    Ben

  7. hamish
    Permalink to comment#

    Hey Chris,
    How would you add an if empty statement to this? Many thanks
    hamish

  8. Vinicius
    Permalink to comment#

    Thanks Mate! It really saved my life!!!!!!!

  9. Mandip
    Permalink to comment#

    can i get the code to display the rest post of the same category in single page on which the viewer is in?

  10. Permalink to comment#

    thanks a lot for sharing this with us thanks

  11. Permalink to comment#

    Nice. Very well explained.
    Thanks

  12. DSQ
    Permalink to comment#

    Hi there, can anyone please tell me how to query custom posts form a certain category? I’ve tried everything. Thanks!

    • Ryan
      Permalink to comment#

      I’m also looking for the same answer as DSQ, when I’m running query_posts – this quires all posts in the db instead of the category you are on… Presuming because it’s outside the loop, or because there is no loop.

      Is there any way you can query the loop? So for example I have a category page and I only want to display posts with the tag ‘example’ in that category, is this possible?

      I’ve been at it for hours! Also just found this which has given me some new ideas http://wp.smashingmagazine.com/2009/06/10/10-useful-wordpress-loop-hacks/

  13. Elise
    Permalink to comment#

    Is there a way to do this so that it puts all the post from one category under that category and then loops to do the same for the next category. I Basically want it to do some think like this: Category 1 – Post 1 – Post 2 – Post 3 Category 2 – Post 1 – Post 2 – Post 3 Category 3 – Post 1 – Post 2 – Post 3 etc.

  14. Linda
    Permalink to comment#

    I used the almost the same code as advised on this post, minor tweaks
    <?php query_posts('showposts=2&category_name=slug'); ?>
    so, showposts sets how many posts are displayed
    category_name=slug works great now that category ID has been deprecated.

  15. Totally used this today, but modified it a bit as I needed to include all posts except the ones that are in the home page slider category. To do this, just add a minus sign in front of the integer like so:

    <?php query_posts('cat=-5'); ?>
    

    Thanks for the post!

  16. Brian
    Permalink to comment#

    Is there a way to have a post, rotate at midnight or daily from a specific category and not repeat for a year?

Leave a Comment

Current day month ye@r *

*May or may not contain any actual "CSS" or "Tricks".