Category Mod for WP Loop
On several different occasions I’ve found the need to modify the WP Loop to produce a desired effect….such as only retrieving posts from a certain category. You can find many examples of how to do this by simply doing a Google search. The most common mod I’ve found is this:
<?php query_posts(’category_name=special_cat&showposts=10′); ?>
<?php while (have_posts()) : the_post(); ?>
<!– Do special_cat stuff… –>
<?php endwhile; ?>
This isn’t just some thrown together code from a lonely developer, it’s the actual snippet from the official WP Codex. I however have never been able to get it to work. No matter what I substitute in for “category_name“, I get all posts. I normally just modify this a little and use:
<?php query_posts(’cat=cat_id&showposts=10′); ?>
<?php while (have_posts()) : the_post(); ?>
<!– Do special_cat stuff… –>
<?php endwhile; ?>
Where “cat_id” is set to the numeric category ID stored int he MySQL DB. This proves to be cumbersome for anyone who doesn’t have direct access to the DB. One way to find out the category ID without DB access is to view the source of a page within a browser and look for the ID there (provided your WP Loop is set to display categories). The use of the “special_cat” tag is suppose to enable the use of the category “slug”, or textual short name for the category (ie. the “News” category might have a slug of “news”). You can find the category slug when editing the category from the admin interface. The us eof the “slug” over the “ID” is much more convenient to most users. I have found a little more modification of the loop from the Codex offers a solution (have tested this on a WP 2.8 & WPMU 2.7.1 install, please let me know if there are potential problems I’m missing by using this):
<?php query_posts(’category_name=cat_slug&showposts=num_posts‘); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?><!– Code for Post Display –>
<?php endwhile; endif; ?>
Where “cat_slug” is equal to the Category Slug, and “num_posts” is equal to the number of posts from that category you want to display.
Because I normally hate people that just leave an example with “<!– Code for Post Display –>” rather than actually showing me what goes in there, I’ll give you an example of this modified loop used within a page:
<?php query_posts(’category_name=News&showposts=5′); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<!– Begin Code for Post Display –>
<DIV class=”post” id=”post-<?php the_ID(); ?>”>
<h1><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></h1>
<DIV class=”entry”>
<?php wp_link_pages(array(’before’ => ‘<p><strong>Pages:</strong> ‘, ‘after’ => ‘</p>’, ‘next_or_number’ => ‘number’)); ?>
</DIV>
</DIV>
<!– End Code for Post Display –>
<?php endwhile; endif; ?>
This modified WP Loop displays 5 posts form the “News” category, Titles only that link to the actual individual post page, and pagination showing if more than 5 posts exist. I hope this helps someon out in the future! Please leave feedback of others possibilities to obtain this same feature, your troubles, or links to your site where you use something similar.
-Phenix
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.




I’m POSITIVE this will help me with all my content! …if I ever spend the time to actually do it (lazy).