How to Show Different Number of Posts on Home and Sub Pages on Genesis Child Theme. [Specially Eleven40 Theme]

When it comes to customizing your Genesis child theme, flexibility is key. A common customization is to display a different number of posts on the first page of your blog compared to the subsequent pages. In this tutorial, we’ll show you how to display 4 posts on the first page and 6 posts on the remaining pages using a custom function.

Step 1: Understanding the Requirements

The goal here is to create a unique look for the first page of your blog by displaying only 4 posts, while the rest of the pages will show 6 posts each. This can create a visually appealing distinction and enhance user experience.

Step 2: Accessing the Functions File

To implement this customization, we need to edit the functions.php file within your Genesis child theme.

Note: Always back up your theme files before making any changes, or work in a staging or local development environment to prevent any unintended disruptions to your live site.

Step 3: Writing the Custom Function

Add the following code snippet to your child theme’s functions.php file:

add_action( 'pre_get_posts', 'custom_posts_per_page' );
function custom_posts_per_page( $query ) {
    if ( $query->is_main_query() && !is_admin() && is_home() ) {
        if ( $query->is_paged ) {
            $query->set( 'posts_per_page', 6 );
        } else {
            $query->set( 'posts_per_page', 4 );
        }
    }
}

Explanation:

  1. Action Hook: We’re using the pre_get_posts action hook, which allows us to modify the query before the posts are retrieved.
  2. Main Query Check: The condition $query->is_main_query() ensures that we only affect the main query and not any secondary queries that might be used on the page.
  3. Admin & Home Page Check: !is_admin() && is_home() ensures that the code only runs on the home page and not in the WordPress admin area.
  4. Page Check: $query->is_paged checks if the query is for a paged result. If true, we set the posts per page to 6; otherwise, we set it to 4.

Step 4: Testing the Changes

After saving the file, be sure to test the changes on various pages to ensure that it works as intended. Make sure to clear any caching plugins if you are using them to see the changes.

Conclusion

Customizing the number of posts per page on your Genesis child theme can be a unique way to differentiate your home page. With a simple code snippet added to your functions.php file, you can create a distinct layout that fits your site’s design and purpose.

Remember to always test changes in a safe environment, and happy coding!

Leave a Comment