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.
  • http://blog.theunical.com Satish

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

    blog.theunical.com

  • http://davur.net Davur

    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?

  • http://www.clickonf5.org Tejaswini

    @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.

  • http://www.gtricks.com Aery

    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?

  • http://www.clickonf5.org Sanjeev Mishra

    @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

  • lala

    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…

  • http://www.clickonf5.org Tejaswini

    @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

  • http://matthew.gedeon.name/ Matt

    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.

  • http://mypositivechange.com/blog Anf

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

  • http://www.vaseemansari.blogspot.com vaseem ansari

    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

  • Sheridan

    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!

  • http://www.webpaz.com webpaz

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

  • http://zapperonline.com henry

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

  • http://www.tagsengine.com Asim

    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.

  • http://php-mysql-guide.blogspot.com/ PHP MySQL Guide

    Very helpful article

  • http://www.bisms.ir/ اس ام اس

    Thank you
    You explained very fully
    I bookmark your site