Show Posts From Selected Categories On WordPress Blog Homepage

We do have a set of categories in which we classify the posts, and by default, all the new posts recently posted from all the categories appear on the home page of our WordPress blog. If you wish to show to your home page visitors only the new posts from some specified categories, that you specialize writing on, this is how you can do that…

wordpress_theme_editor In the wp-admin section of your WordPress blog, go to the Appearance => Editor => home.php (if exists) or index.php (if home.php does not exist). You would get the Appearance tab on the left hand side of the admin panel and the template files on the right hand side.

In the home.php or index.php code, find out the location of the WordPress Loop i.e.

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

Before the start of the WordPress loop, add the below code

<?php if (!is_paged() and is_home()) {
      query_posts('cat=519,186,2'); 
      wp_reset_query();
} ?>

wordpress_categoriesWhere, the numbers 519, 186 and 2  in cat=519,186,2, represent the category IDs of the categories that you want to show on the home page.

You can find the category IDs of the required categories by going to the Posts => Categories panel of the admin screen. Hover on any category Name appearing on the right hand side of the panel, and you would see the Category ID in the link of the category in the browser status bar.

 

hover_category_name

Below is the screenshot of the browser status bar showing the WordPress category ID.

category_id_wordpressThus, get all the category IDs of the categories from which you wish to display the post on the homepage of your WordPress blog, and put in in the above mentioned code. You can add as many category IDs as you wish, by just comma separating them. e.g. ‘cat=519,186,2,200,156′

Important Points here are:-

  1. Do not forget to add wp_reset_query() function after the query_posts(). There is a small issue with query_posts() function, that is it causes some changes to the wp_query conditional tags like is_home() would not work and so on. I have got this kind of issue, and after digging on it, found that query_posts() is the culprit. You can just add the wp_reset_query() after call to query_posts() and see it would solve the issue.
  2. This code would display the posts from the selected categories on the blog home page and not on the next pages shown by clicking the ‘Next Page’ link. Thus, as the main page is being split up over several pages, this code will show the posts from all the categories on 2nd and subsequent pages of posts.

16 Comments

  1. Satish

    September 27, 2009 at 12:18 pm

    that’s a good example i have implemented that in my blog only featured list will be displayed in blog

    blog.theunical.com

  2. Davur

    September 28, 2009 at 7:19 pm

    I just tried that out. Didn’t quite work. It worked at first, but then I added a few more posts to the ‘home category’ and it stopped working.
    I think it was because is_paged() started off as false and then became true, which made your code not execute. Does that make sense?

  3. Tejaswini

    September 29, 2009 at 11:40 am

    @Davur This code would work only for the home page, and there is no impact if you add more posts to the specific category displayed. Have you added wp_reset_query() at the right position and also is the category number of the “home category” correctly specified in the query_posts?I am just trying to locate the exact cause.

  4. Aery

    October 12, 2009 at 10:15 pm

    Hi Sanjeev,

    Instead od selected category. can I have particular tag to be displayed in home page?

    And what are the advantages of using this technique?

  5. Sanjeev Mishra

    October 14, 2009 at 6:18 pm

    @Aery Thanks for posting your query. I have created a thread dedicated to your question on Internet Techies Forum. Please have a look. http://www.clickonf5.org/phpbb/blogging-tips-f17/use-tag-instead-category-home-page-t44.html

  6. lala

    October 29, 2009 at 7:27 pm

    I’m trying to use this code but its not working. I put it at the right place but posts under other categories are still showing up at the front blog page. Would you please tell me what I’m doing wrong? Thanks…

  7. Tejaswini

    October 30, 2009 at 1:54 am

    @lala Ya definitely, I would try my best to get the issue resolved with the query code on your wordpress site. I have created a new forum thread at Internet Techies forum, can you please join that as you could post the code in issue and your website there in readable format.
    Forum thread:- http://www.clickonf5.org/phpbb/code-issues-f20/show-posts-from-selected-categories-wordpress-blog-homepage-t67.html

  8. Matt

    January 22, 2010 at 8:59 am

    For those who are having trouble making this code work, try deleting the query reset command. It seems that line of code is no longer needed. Took me a while to figure out what the problem was.

  9. Anf

    March 8, 2010 at 5:46 pm

    What if I want to display all posts from one category on a new page (not home page)? How would I do it?

  10. vaseem ansari

    March 24, 2010 at 7:08 pm

    i want to show all posts from particular category.
    i have url like this
    http://server/wordpress/?page_id=31

    now will u plz tell me how can i do this.
    i want all the posts from that particular category will be shown on post page, u must know that we can do this work b using archive.php page. but as i have url like above so how can i show the posts using page.php.

    Thanks

  11. Sheridan

    April 26, 2010 at 11:06 pm

    I like this concept. This is what I want, however can I use this to display the most recent post from a category in the sidebar rather than on the home page? — Thanks!

  12. webpaz

    April 29, 2010 at 12:25 am

    I put it at the right place but posts under other categories are still showing up at the front blog page

  13. henry

    June 5, 2010 at 7:37 am

    Hey, nice stuff.. keep it up. :D

  14. Asim

    June 22, 2010 at 8:27 pm

    This will help me for showing post around the home page. Currently i am using almost 9 widget to show post from certain catergories using pluign to list the post from catergory.

  15. PHP MySQL Guide

    June 29, 2010 at 2:35 pm

    Very helpful article

  16. اس ام اس

    November 24, 2013 at 7:18 pm

    Thank you
    You explained very fully
    I bookmark your site

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>