How to Show WordPress Posts from a Single Category Only

If you’ve ever wanted to show only posts from a single WordPress category somewhere, such as on your homepage or in a template file that your create, then it’s pretty easy to do; however, it does take a little digging into your theme’s files.

Editing Your Theme’s Files

In order to display posts from one category, you’ll want to find “The Loop” in the file where you want those posts to display. In the examples below, we’re going to be putting our code into our homepage.

In many themes, you will easily be able to find The Loop for your homepage in your index.php file. (Appearance > Editor > Main Index Template – index.php)

The Loop is the code that begins like this:

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

Some themes these days put the loop in a separate file and call it into the index.php file with other code. An example of this can be found in the default WordPress TwentyTen theme.

If you have a case like that, you can just find the spot where the loop is being called in, and place your code above that.

Here’s a look at the code in the TwentyTen theme. It shows you where you can place your code.

Code for Showing Posts from Only One Category

There are a number of ways you could achieve this, but perhaps these two are the easiest: You can choose to pull the category posts in via the Category ID number or the Category Name.

We’ll start with using the category name.

Let’s say you have a category titled “Foreign Cars.” Here’s the code for that.

<?php query_posts('category_name=Foreign Cars’); ?>

And here’s what it looks like in my theme’s file. (Note: I’m not using the TwentyTen theme here, so you can see the actual beginning of The Loop.)

 

As mentioned, you can also achieve the same thing by using the Category ID Number. If you aren’t sure how to find the ID, see the following post: How to Find the IDs of WordPress Categories, Posts, Pages, and More.

When you have your ID number, then insert code like the following (replacing the number with the number of your category). In this example, my category ID is 14.

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

Showing Posts from More than One Category

Either of the methods above will show the posts from only the category you choose, but what if you’d like to show posts from two categories (or three or four)?

That’s easy enough to do. You simply take one of the methods above and add a little more code to it. In this example, let’s say I want to show posts from my category titled “Foreign Cars” and my category titled “Fast Boats.”

Here’s how I’d do that:

<?php query_posts('category_name=Foreign Cars, Fast Boats’); ?>

And if I’m using category IDs instead of category names, here’s how I’d do it.

<?php query_posts('cat=14,15'); ?>

Control Number of Posts

OK, so the code above should get you the categories you’d like, but you can get a lot more control than that by adding a few other parameters.

One of the most common that someone might like is to control the number of posts you want to appear. So, for example, you might want your Foreign Cars category to show, but you only want three posts to show (not the default ten).

In order to achieve that, you’d have code like this (add &showposts=3):

<?php query_posts('category_name=Foreign Cars&showposts=3'); ?>

Change the number 3 to whatever number you like.

 

If you’re using Category IDs, then the code would look like this (again, just add &showposts=3):

<?php query_posts('cat=14&showposts=3'); ?>

And you can also do this even if you’re showing multiple categories as in the example above (add &showposts=3):

<?php query_posts('category_name=Foreign Cars, Fast Boats&showposts=3’); ?>

Using category IDs it would look like this:

<?php query_posts('cat=14,15&showposts=3'); ?>

Note: In the example above, this will NOT pull three posts from each category. It will only pull three posts total (the latest three posts).

Filter by Tag

Another popular parameter you may want to filter by is tags. Here are a few examples of that. We’ll only pull posts from the Foreign Cars category that are tagged “red cars” (note that we put a hyphen in for tags if it’s two or more words)

<?php query_posts('category_name=Foreign Cars&tag=red-cars’); ?>

And using category IDs it would look like this:

<?php query_posts('cat=14&tag=red-cars’); ?>

Below are some more popular parameters you may want to use.

 

Show Posts in Ascending Order

By default, WordPress shows post in descending order. In other words, it starts with the last post and puts it at the top (it descends down from a high numbered post to the lowered numbered posts). If you like to reverse that, you’ll need to tell WordPress to show posts in ascending order (starting with the lowest numbered post).

<?php query_posts('category_name=Foreign Cars&order=ASC’); ?>

Or

<?php query_posts('cat=14&order=ASC’); ?>

Show Posts in Alphabetical Order (A – Z) by Title

<?php query_posts('category_name=Foreign Cars&orderby=title&order=ASC’); ?>

Or

<?php query_posts('cat=14&orderby=title&order=ASC’); ?>

Show Posts in Reverse Alphabetical Order (Z – A) by Title

<?php query_posts('category_name=Foreign Cars&orderby=title’); ?>

Or

<?php query_posts('cat=14&orderby=title’); ?>

Filter Posts by Author

This will pull only posts from this author (Bill) in the selected category (Foreign Cars).

<?php query_posts('category_name=Foreign Cars&author_name=Bill’); ?>

Or using the category ID and the author ID:

<?php query_posts('cat=14&author=3’); ?>

Stacking Parameters

As you can see, you can add lots of parameters to filter the posts. You can play around with them until you get what you want.

For example, let’s set up some code to do the following:

  • Pull only posts from the Foreign Cars category (category_name=Foreign Cars)
  • By the author named Bill (&author_name=Bill)
  • With a tag “red cars” (&tag=red-cars)
  • Let’s show only five of them (&showposts=5)
  • And lets order them so that the oldest one shows at the top (&order=ASC)

And here’s our code for that:

<?php query_posts('category_name=Foreign Cars&author_name=Bill&tag=red-cars&showposts=5&order=ASC’); ?>

Pull in Your WordPress Pages

Another little related trick will let you pull in a Page onto the front page. You would do that like this with the Page ID (in this example, the ID is 22):

<?php query_posts('page_id=22'); ?>

If you’d like to pull in multiple pages, then it’s a little more complicate. Let’s pull in Page 22 and Page 25.

<?php
$args = array(
'post_type' => 'page',
'post__in' => array(22,25)
);
query_posts($args);
?>

Now that you have the hang of this, you can check out a wordpress.org page where you can find even a few more parameters.

Photo: Export Icon from BigStock

Related posts:

  1. Featured WordPress Plugin: List Category Posts List Category Posts lets you list posts from a specific…
  2. Daily Tip: How to Show Only the Author’s Posts in the Edit List Here’s a quick code snippet that will restrict the posts…
  3. Export One Category or Posts from Different Categories with WordPress If you only need to export parts of your site,…