Using the WordPress loop to display just one category on the home page
In this tutorial we set the home page to display our latest posts. Then we use a code snippet, provided by Bill Erickson (WordPress developer), to force the home page to only display posts from one category.
Only do this if you’ve backed up your site and can restore the site using FTP
The industry Standard for WordPress Themes
(See slides at bottom of this page)
Go to this page on Bill Erickson’s website…
http://www.billerickson.net/customize-the-wordpress-query/
This page explains in detail about Customizing the WordPress Query.
We’re going to copy the code from Bill’s site, so scroll down the page to the section that says… “Exclude Category from Blog” and copy the code.
This is the code that appears on Bill’s site.
Do NOT copy the <?php tag.
In your StudioPress theme, go to…
- Dashboard > Appearance > Editor.
- Then click Theme Functions (functions.php)
- Paste the code at the very bottom of functions.php
- Click the Update File button.
Now we need a category to display on our front page.
- Go to Dashboard > Posts > Categories
- Make a category called Home Page.
- Put some posts in this new, Home Page, category.
- Hover over the category name and look at the bottom of your screen for some code to pop up. Find the id=
- Make a note of the id= number for your category.
Go back to functions.php Look at the bottom of the code you pasted from bill Erickson’s site and find…
$query->set( ‘cat’, ‘-4‘ );
Change -4 to the category number for your Home Page category.
I made a category called Home Page, this category had an id of 5. So this is how I changed the code…
$query->set( ‘cat’, ‘5‘ );
-4 removes category 4 from your homepage. 4 without the – (minus) adds that category to your home page. To include more than one category use commas.
To display categories 1,4 and 6 on the home page, I’d do this…
$query->set( ‘cat’, ‘1,4,6’ );
You don’t need a comma after the last id number, or if you only want to display one category.
Make sure you don’t delete the single quotes around the number…
$query->set( ‘cat’, ‘5‘ );
And don’t forget to save your changes at each step of the process.