In a comment on Genesis Starter Child Theme, a user asked,
Thank you for the changes, they are great! In the Genesis > Theme Settings there is a ‘blog page template’ section that allows you to select to show only posts from a specific category. However, with the changes there is no blog page template now so how do I still show only recents posts from one category on the home page?
Thanks to this wonderful post by Bill Erickson, we know that pre_get_posts
is the way to go in this situation.
Adding the following in child theme’s functions.php shows only Posts from a particular category on the Posts page (this would be the homepage by default):
add_action( 'pre_get_posts', 'sk_show_posts_from_a_category_posts_page' ); | |
/** | |
* Show Posts from a specific category on Posts page | |
* | |
* @author Bill Erickson | |
* @author Sridhar Katakam | |
* @link http://www.billerickson.net/customize-the-wordpress-query/ | |
* @param object $query data | |
* | |
*/ | |
function sk_show_posts_from_a_category_posts_page( $query ) { | |
if( $query->is_main_query() && !is_admin() && is_home() ) { | |
$query->set( 'category_name', 'category-1' ); // Replace "category-1" with your category slug | |
} | |
} |
Source: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
If i want to do this for other pages like say : Eateries Page and Events Page. Would i write it like below? page; http://tracywelch.com/around-town/eateries/
if( $query->is_main_query() && !is_admin() && is_eateries() ) {
$query->set( ‘category_name’, ‘eateries’ ); slug } }
if( $query->is_main_query() && !is_admin() && is_events() ) {
$query->set( ‘category_name’, ‘events’ ); slug } }
No.
What sort of pages are these? CPT archives?
when i made pages i chose blog.
This works in each and every theme?
[…] How to limit Posts on Posts page to a specific Category in WordPress […]
Is it possible to define multiple categories here?
any special coding support for AMP