Make WP show only one category on the front page
This is virtually identical to my previous post on how to make WP show only one post on the front page, but I felt I might as well post it so that:
- I have a set of straightforward, step by step instructions that I can direct people to
- To make it easier for people to find an answer via search engine.
Someone on the WP support forum asked if it was possible to only show only posts from a single category on the front page. Her intention was to use a category purely as an archive category.
As mentioned before, the instructions should be fairly familiar.
First you’ll need to make a copy of the index.php file in your theme directory. Rename this file home.php. This file will take precendence over index.php and be used for the front page display.
Now, at the top of this file, you’ll need to make a call to query_posts and utilise one of the parameters I had previously blogged about.
<?phpget_header();query_posts('category_name=main'); //where main is the name of the category that you want to show.?>
This will ensure that only posts that have been posted to the category main are displayed.
Exclude a category instead of include
What if instead of only showing one category on the front page, you want to exclude a particular category from the front page. This is fairly straightforward as well.
Firstly, you'll need a home.php file as described above.
Then you'll need to get the unique id of the category that you want to exclude. You can get this by navigating to [YOUR_BLOG_LOCATION]/wp-admin/categories.php. There should be a listing of the category and their associated id's.
Now open up the home.php file and at the top of the file, what you should use instead is:
<?phpget_header();query_posts('cat=-1'); //where 1 is the id of the category that you previously found?>
This will exclude all the posts that belong only to category 1. There is a proviso with this method however, and it’s a fairly big one. Posts in the category that you want to exclude must only belong to this category.
Another way to exclude a category
If that proviso was too severe, then you can make use of one of the WP conditionals. Once again, make a home.php file and around the display of the post related content, you can wrap it in an in_category conditional.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if (!in_category('1') ) { ?>***do your regular stuff***<?php } ?><?php endwhile; else: ?><?php endif; ?>
The main problem with this method (and why I'm not a fan of it) is that you will find that the number of posts being excluded counts towards the number of posts being retrieved.
-30-
maybe i’m all mixed up, but the first way isnt working for me..
i renamed the index.php that i had in the theme folder to home.php and added that query that you said, and it basically still showed posts from category 2 on that page…
“i renamed the index.php that i had in the theme folder to home.php”
Did you copy (not rename) the theme’s index.php and add the following?
query_posts('category_name=Poetry');i.e. there should be a home.php in the
/wp-content/themes/chewinggum/ folder.
how about this -> i was try the plug in and make WP show only one category on the front page
http://www.gudlyf.com/media/category_visibility.phps
copy the “coding–>save as php files> save to plugin directory>actived on admin>go head!
[…] I’ve recently received, like phu, inquiries as to how I created the template to drive the front page. […]
Thanks I couldn’t remember how to do this little trick.
[…] Make WP show only one category on the front page […]
Thanks for the info. I am trying something similar, but slightly different. I would like to have a few items on the index.php page…
-regular posts, (with excerpts instead of the full post)
-category list from category A
-category list from category B
-category list from all categories, or at least most, limiting some with the list_cats exclude option
I can do everything as listed above, but I am using the list_cats exclude argument to show only 1 category in a single section. This is, to say the least, inelegant. Can you please offer any advice to help me achieve the criteria above?
P.S. love the look!