How to Display Category Post on WordPress Blog Homepage

Advertisement

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.

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.

You can also use FTP to open home.php or index.php inside the theme that you are selected. (Recommended for advance user or developer)

In the home.php or index.php code, search this line of code or wordpress loop.


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

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


<?php if (!is_paged() and is_home()) {
      query_posts('cat=1');
} ?>

Note:

where 1 is the category ID to be displayed or the category ID you want to show on blog homepage.

You can find the category IDs of the required categories by going to the Posts => Categories panel of the admin screen. Hover on any categoryname 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.

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

You can tweak all categories to be displayed on the homepage by replacing the query_posts() code above in the code below.

Display posts that have these categories, using category id:


query_posts( 'cat=2,6,17,38' );

Display posts that have these categories, using category slug:


query_posts( 'category_name=staff,news' );

Display all posts except those from a category by prefixing its id with a ‘-‘ (minus) sign.


query_posts( 'cat=-3' );

Display posts that are in multiple categories. This shows posts that are in both categories 2 and 6:


query_posts( array( 'category__and' => array( 2, 6 ) ) );
Advertisement