This code enables you to show a different number of posts on the front page as opposed to whats displaying on your blog page and other archives.
It also enables you to remove posts from specific categories from displaying on the front page.
This example assumes your Reading Settings are set to display 3 posts and your Genesis > Theme Settings > Blog Page Template also set to display 3 posts.
Reading Settings
Blog Page Template
These settings apply to any page given the “Blog” page template, not the homepage or post archive pages.
function exclude_category_frontpage_limit( $query ) {
if ( $query->is_home() && $query->is_main_query() && !is_admin() ) {
$query->set( 'cat', '-27' );
$query->set( 'posts_per_page', 1 );
}
}
add_action( 'pre_get_posts', 'exclude_category_frontpage_limit' );
Change the category I.D in the above code from -27 to match the category I.D you want to exclude from the front page.
If you only want to display posts from specific categories, change the code to something like this:
function include_category_frontpage_limit( $query ) {
if ( $query->is_home() && $query->is_main_query() && !is_admin() ) {
$query->set( 'cat', '1,2,3' );
$query->set( 'posts_per_page', 1 );
}
}
add_action( 'pre_get_posts', 'include_category_frontpage_limit' );
This code only displays posts from categories with the I.D’s 1,2 and 3.
Note: The is_front_page() conditional tag will not work with the pre_get_posts function.
Video Demonstration
This video shows you what happens when you add this PHP code to your child themes functions.php file.
Based on the above settings used in this example, your front page will display 1 post and your blog page will display 3 posts.
This code filters the WordPress Reading Settings which limit the amount of posts displayed on the front page rather than the Genesis blog page limit which is controlled by the Genesis Blog Template settings.
This solution answers this question from a member of WP Sites.
I want to reduce the number of posts displayed on the homepage without it affecting the number of posts I have told Genesis to show on my blog page, and to show a different sidebar as opposed to the primary sidebar.
It would also be really useful to show posts from a certain category on the homepage, as opposed to all posts.
Tested on the Education Pro child theme by StudioPress.
Was This Tutorial Helpful?