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.

Leave a Reply

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