• Downloading from our site will require you to have a paid membership. Upgrade to a Premium Membership from 10$ a month today!

    Dont forget read our Rules! Also anyone caught Sharing this content will be banned. By using this site you are agreeing to our rules so read them. Saying I did not know is simply not an excuse! You have been warned.

How to Show WordPress Posts from a Single Category Only

Admin

Well-Known Member
Staff member
Administrator
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:

Code:
<?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.
when-loop-is-called-into-index.jpg



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.
Code:
<?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.)

insert-query-post-display-one-category.jpg


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.

Code:
<?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:

Code:
<?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.
Code:
<?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):

Code:
<?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):

Code:
<?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):

Code:
<?php query_posts('category_name=Foreign Cars, Fast Boats&showposts=3’); ?>
Using category IDs it would look like this:

Code:
<?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)

Code:
<?php query_posts('category_name=Foreign Cars&tag=red-cars’); ?>
And using category IDs it would look like this:

Code:
<?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).

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

Code:
<?php query_posts('cat=14&order=ASC’); ?>
Show Posts in Alphabetical Order (A – Z) by Title


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

Code:
<?php query_posts('cat=14&orderby=title&order=ASC’); ?>
Show Posts in Reverse Alphabetical Order (Z – A) by Title


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

Code:
<?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).

Code:
<?php query_posts('category_name=Foreign Cars&author_name=Bill’); ?>
Or using the category ID and the author ID:

Code:
<?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:

Code:
<?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):

Code:
<?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.

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

Facebook Comments

Similar threads
Thread starter Title Forum Replies Date
Kyxaoviet 3.46GB Vinh danh luxury award show psd Googledriver Pikbest freepik Thiết kế đồ họa 0
Admin Yilmaz - Slide Show vB5 Add-ons 0
Admin [OzzModz] Show Pagination On Mobile Device Xenforo 0
Admin Hướng dẫn tạo cảnh báo cho Xamarin Forms giống MessageBox.Show trên C# Android, iOS 0
Admin [OzzModz] Show Banned xenforo 2 Xenforo 0
fsend.vn Trực tiếp show nhạc Trịnh NHỚ MÙA THU HÀ NỘI lúc 20h| 27.08.2016 Âm nhạc 0
Admin [FsVN] The Victoria Secret Fashion Show 2013 HDTV x264-2HD - Đại Tiệc Nội Y (2013) Phim 1
Admin e360 Always Show Your Signature Add-ons 0
Admin [FsVN] The Victoria Secret Fashion Show 2013 HDTV x264-2HD - Đại Tiệc Nội Y (2013) Phim 1
Admin Hiển thị thành viên trực tuyến - Show user is online xenforo Xenforo 0
Admin Share module slide show ảnh đẹp cho joomla 2.5 Joomla 3
Admin [Video] tham quan Sony Show 2013 Tin tức CNTT 0
Admin Share code php show bài viết chuyên mục có chia trang PHP 0
D Share Show Mod Games Multi ScreenOf JohnCms Johncms 0
Admin Share code show bài theo chuyên mục code v5 Mã nguồn wap 0
Admin Share code auto wap web show list forum Johncms 0
Admin [ozzy47] VSa - ChatBox Show Active Users Addon Vbb tutorial 0
Admin Share code làm trang show ảnh hoặc trang cá nhân HTML & CSS 3
Admin Yilmaz - Slide Show and Hotbar Add-ons 0
blog4me [BLOG4ME] Show anh chi gai tui Ảnh thành viên 20
nhokzodanh top show ảnh thành viên ttvn Hình ảnh 25
Kidblood Share Show con hàng cf--- ai kết bán luôn giá gốc Trò chơi 0
djdungcuty Show mạch amly với TDA2030A Video, clip 2
Admin Share mod nivo slide show - tạo slideshow quảng cáo cho vbb cho vbb Vbulletin 0
H Help Admin hd em cach phat the va show phat the ra khng post bai Vbulletin 4
Admin Share code photo album cực đẹp cho ai thích show ảnh HTML & CSS 0
Yeukodamnoi Game The Hangman Show Biểu diễn ảo thuật thắt cổ Android, ios, java, windows phone 0
boy94 show anh nguoi yeu Hình ảnh 7
S Show categories thành 2 cột Xenforo 1
Admin Hướng dẫn tạo chuyên mục thành 2 cột - Show categories in two columns (giống tinhte.vn) Xenforo 0
Admin Share auto wap/web show list forum Johncms 0
Admin [XenForo] ChipXf - Show smilies on thread title (fixed) Xenforo 0
Admin Share Code Show Album Cực đẹp Mã nguồn web 4
S Show and Help Thảo luận chung 4
congtust24 Chi tiết và ý nghĩa tất cả các lỗi http (Show All HTTP Error Status Codes) Kiến thức lập trình 1
K Show Skin PC Thui! ^^! Style vbb 3
chickIT Share Show ảnh Flickr Photo trong trang Profile VBB3 Add-ons 0
style Show File .htaccess Trong Host Chuyên đề htaccess 3
T Ai có css wap đẹp show hàng đi. HTML & CSS 5
blog4me Ae vào show điểm thi tốt nghiệp nào Thảo luận chung 12
style Show Lỗi Giacmovn Thùng rác dọn dẹp thường xuyên 6
mày.hả.bưởi Show code và xin ae ý kiến All Shared Scripts 6
style Show Index Nguoibanit.xtgem.com Wap builder, wapego, xtgem, wen.ru, wapka, wap4 4
A Show wap scam Teamobi ! Góc thành viên học tập 3
A Topic show ảnh all member Ảnh thành viên 0
Admin Show ảnh admin xtgem là Graham và Povilas Thảo luận wap việt 6
Admin Show full anbum admin Mạnh Hùng daivietpda dự offline kichhoat 28/4 Thảo luận wap việt 1
Admin Share code show ảnh đẹp Mã nguồn web 0
Q quangthjeugia chao ae.ae vao show anh nao Ảnh thành viên 3
Admin Show mobile view forum khi dùng opera4.x Thảo luận wap việt 12

Similar threads

New posts New threads New resources

Back
Top