Tutorials WordPress Show Posts from Specific Categories to Appear on WordPress HomePage

Show Posts from Specific Categories to Appear on WordPress HomePage

Quick WordPress code snippet to show only the posts from specific categories to appear on homepage recent posts. The landing page of most of the news or personal blogging sites usually display the recent posts achieve of the blog unless the front page is set to a static HTML page.

Your blog might have many different categories, but could be annoying to users who just want to show the recent posts from specific categories to appear on homepage recent posts. The following code snippet will help you to do that.

Add the following snippets to your WordPress theme functions.php file:

function filter_home_post_categories($query) {
    if ($query->is_main_query() && is_home()) {
        $query->set('category_name','cat_slug1, cat_slug2, cat_slug3');
    }
}
add_action('pre_get_posts', 'filter_home_post_categories');

Save the changed andfunctions.php file and visit your WordPress site home page. Notice that you will see only the posts from the categories as specified in the above function.

Total
0
Shares
guest
0 Comments
Inline Feedbacks
View all comments
Previous Post
Angular JS Features

Key Features of AngularJS That Makes The Future of Web Development

Next Post
Wordpress Tutorials

Include Custom Post Types in Your WordPress RSS Feed

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
0
Would love your thoughts, please comment.x
()
x