Set Post & Content Limits For Home, Blog & Archive Pages

In this tutorial we’ll take a look at the built in Genesis settings you can use to control the content limits in your archives and blog page template.

We’ll also take into consideration the WordPress reading settings and how they effect the Genesis settings.

On top of this, we’ll also look at some working code snippets you can use to further customize your:

  1. Blog Page
  2. Archives
  3. Home page
  4. Grid loop
  5. Custom Post Types

We’ll use the new Eleven40 Pro child theme by StudioPress as well as the Genesis 2.0 Sample child theme which doesn’t include any grid loop or home page widgets out of the box.

The Eleven40 Pro theme along with several other StudioPress themes like the Balance theme include a custom grid loop in the home.php file.

Reading Settings in WordPress

Firstly, lets look at the existing settings WordPress provides in the Reading Settings. These settings will effect how many posts display on all your archive pages including:

  • Blog listings page
  • Archive pages including author, blog, category, search, and tag pages

wp reading settings

The reading settings determine how many posts are displayed on all your archives pages and home page if left at the default settings like you see in the above screen shot.

The Reading Settings do NOT control your blog page as this is controlled by the Blog Page Template settings Genesis includes under the Genesis > Theme settings:

genesis blog page template

Lets look at an example of this: If your Reading Settings are set to display 5 posts, your home page and all archives will display 5 posts however your blog page will display whatever you set it to display.

Even though the Reading settings might be set to: Blog pages show at most 5, this only applies to the home page and NOT when using a static custom page with the blog page template for your custom blog page.

These settings are controlled by the genesis blog page template settings as shown in the screenshot above.

Customizing Default Home Page Post Limit

If you want your archives to display the number set in the Reading Settings but display a different number of posts on your home page, you will need to use some custom code in your child themes functions.php file:

add_action( 'pre_get_posts',  'change_posts_number_home_page'  );
function change_posts_number_home_page( $query ) {

if ($query->is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 5 );

return $query;
}
}

The above code won’t effect the number of posts displayed on your archives.

Change Number Of Posts Displayed On Archives

You can also use this code to change how many posts display on different archives like category and author archive pages without effecting the Reading Settings which control the others:

add_action( 'pre_get_posts',  'change_number_posts_per_category'  );
function change_number_posts_per_category( $query ) {

    if ( is_category('29') ) {
        $query->set( 'posts_per_page', 1 );

    return $query;
}}

Customizing Grid Loop Post Limit On Home Page

If your theme includes a grid loop in the home.php file, you can simply control how many full width featured posts and how many grid loop posts are display on your home page.

On top of this, you can also control the content limit for both featured posts and posts displayed in the grid only on the home page as the code is included in the home.php file.

All the settings are in the home.php file grid loop code:


remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'eleven40_grid_loop_helper' );
/** Add support for Genesis Grid Loop */
function eleven40_grid_loop_helper() {

    if ( function_exists( 'genesis_grid_loop' ) ) {
        genesis_grid_loop( array(
            'features'              => 1,
            'feature_image_size'    => 0,
            'feature_image_class'   => 'alignleft post-image',
            'feature_content_limit' => 0,
            'grid_image_size'       => 'grid-featured',
            'grid_image_class'      => 'grid-featured',
            'grid_content_limit'    => 250,
            'more'                  => __( '[Continue reading]', 'eleven40' ),
            'posts_per_page' => 5,
        ) );
    } else {
        genesis_standard_loop();
    }

}

genesis();

The PHP code above is set to display 5 posts. Starting with one featured post which displays full content width and unlimited content limit as its set to 0 so it displays the full post content of the first post.

It then displays 4 posts in a grid with each set at a content limit of 250 with featured image if you have uploaded one for each post and set it as a featured image.

These settings are overwritten by the Reading Settings meaning if your Reading Settings are set to display 20 posts, that’s what your home page will display regardless of the settings in the above grid loop.

To customize this, you can use this PHP code below in your child themes functions.php file to overwrite your Reading settings for your grid loop and therefore use the Readings Settings to control how many posts are displayed on all your archive pages.

Change Grid Loop Posts Limit

add_action( 'pre_get_posts', 'change_num_posts_in_grid' );
function change_num_posts_in_grid( $query ) {
    global $wp_the_query;
        if( $query->is_main_query() && is_home() ) {
            $query->set( 'posts_per_page', '5' );
        }
}

The above code can also be modified to work on any other pages using the grid loop simply by changing the conditional tag in the code from is_home() to something else.

Sub Pages of Home Page

If you also want to control the number of posts displayed on sub pages for grid or standard excerpts, you can use code like this:

add_action( 'pre_get_posts', 'change_num_posts_in_grid' );
function change_num_posts_in_grid( $query ) {
    global $wp_the_query;
        if( $query->is_main_query() && is_home() && is_paged() ) {
            $query->set( 'posts_per_page', '5' );
        }
}
Note: If your home page uses a grid, these sub page archives will also display in a grid.

Learn more about Using The Genesis Grid Loop For Different Home Post Layouts.

Change Content Limit For Specific Category

You can also overwrite the WordPress Reading Settings and customize the excerpt limit on any archive including your home, author, blog, category, search, and tag page archives.

add_filter('excerpt_length', 'specific_category_excerpt_length');
function specific_category_excerpt_length($length) {
    if(is_category('29') ) {
        return 20;
        }
}

The code above will change the excerpt length on the category page with an i.d of 29 to a 20 word limit.

Note: The above code only works when you have set the Genesis > Theme Settings > Content Archives > Display Post Excerpts and won’t work if you use the Display Post Content setting.

content archives - display post excerpts

You can also add back HTML formatting tags stripped from excerpts by WordPress.

On top of this, you can use this code below to control the content limit of excerpts on specific archives and then also set others to display a set limit. Note: Your Genesis > Theme Settings > Content Archives must be set to display Excerpts and not Content Limit.

add_filter('excerpt_length', 'specific_category_excerpt_length');
function specific_category_excerpt_length($length) {
    if(in_category('web-design') ) {
        return 20;
    } else {
    return 500;
    }
}

The above code displays a content limit for excerpts in the web design category of 20 and all other archives display a limit of 500.

Here’s some PHP code which includes an array of multiple categories:

add_filter('excerpt_length', 'specific_category_excerpt_length');
function specific_category_excerpt_length($length) {
    if(in_category(array( 153,154,156 ) ) ) {
        return 100;
    } else {
    return 300;
    }
}

This code uses the category i.d’s and displays a content limit of 100 for 3 categories. All other archives display a limit of 300.

Learn more about customizing excerpts.

Custom Post Type (CPT)

Here’s the code which determines how many posts in a custom post type are displayed on the CPT’s archive page:

add_action( 'pre_get_posts', 'cpt_archive_items_limit' );
function cpt_archive_items_limit( $query ) {

    if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'your-cpt' ) ) {
        $query->set( 'posts_per_page', '3' );
    }

}

Simply replace your-cpt in the code above with the name of your custom post type.

Conclusion

You can see for yourself that using WordPress hooks and filters also work perfectly in Genesis.

Similar Solutions


Comments

27 responses to “Set Post & Content Limits For Home, Blog & Archive Pages”

  1. Michelle Avatar
    Michelle

    Hello Brad- thanks for all your helpful tutorials 🙂

    I am trying to figure out how to change the homepage display so the current blog post only shows an excerpt, and not the whole thing. I’d like to add a “read more” before the end of the article.

    I have tried changing it in the Genesis settings, and Settings > Reading, but those options do not affect the new post on the landing page.

    Any ideas?

    1. Brad Dalton Avatar
      Brad Dalton

      Hello Michelle

      The code is in the genesis_grid_loop in your child themes home.php file.

      You’ll find settings to control the content limit for both features and grid in the code in your home.php file.

  2. V Subramanian Avatar
    V Subramanian

    I would like to know how to have standard length of each page.
    I use Studiopress Galaxy child theme.
    Some sub-sections – the page runs to several standard A4 sizes.I like to see each screen of standard lengths.
    Does one limit by number of words or lines?
    Please help

    1. Brad Dalton Avatar
      Brad Dalton

      Either. The Edit screen displays the word count so you can use that.

      1. V subramanian Avatar
        V subramanian

        Thanks. I am writing a manual of 300+ pages
        I like a setting so that the screen limit is set automatically than me doing it as I write
        Is there an elegant way to do this pn page 1 so that it is
        Followed for 300 pages

        1. Brad Dalton Avatar
          Brad Dalton

          You can use the content limit in the Genesis > Theme Settings > Content Archives or for non Genesis theme users you can use the PHP code to control the excerpt limit.

          [code]
          function custom_excerpt_length( $length ) {
          return 20;
          }
          add_filter( ‘excerpt_length’, ‘custom_excerpt_length’, 999 );
          [/code]

          Another option is to use the wp_trim_words function.

          1. V subramanian Avatar
            V subramanian

            Many thanks
            Unable to understand anything on php or html
            Just text. Go here. Go there. Click this. Click that. At age 74 i am happy in what i know of this technology
            Hope u do not mind explaining the way I understand. If there is a simpler way

          2. Brad Dalton Avatar
            Brad Dalton

            Depends on where you want to limit the content.

            The single post or the archive page?

            If you don’t understand coding, you won’t understand any custom coding solution so i suggest you search the WordPress plugin directory for a plugin solution.

          3. V subramanian Avatar
            V subramanian

            Sorry to waste your time
            Many thanks for your time
            I will find solution elsewhere
            Sorry for not being upto minimum standard

          4. Brad Dalton Avatar
            Brad Dalton

            The solutions i provided are simply a copy and paste job.

            Anything else will require a plugin or custom code.

  3. Hello,

    great tutorial.

    How to make the featured images full width on frontpage?

    Thanks,
    Egis

    1. Brad Dalton Avatar
      Brad Dalton

      Full width of screen or content area or content sidebar wrap?

      1. content sidebar wrap

        1. Brad Dalton Avatar
          Brad Dalton

          If your theme uses a front=page.php file, simply add this code to the file
          [code]
          add_filter( ‘genesis_pre_get_option_site_layout’, ‘__genesis_return_full_width_content’ );
          [/code]

          You will then need to make sure you have set a size in your Media settings which matches the width of your content sidebar
          or
          Add a custom image size in your functions file.

          Then you can align your image and modify the alignment of your image.

          1. I added:
            [code]
            .entry-content img.alignleft {
            float: none;
            display: block;
            margin-left: auto;
            margin-right: auto;
            width: 100%;
            }
            [/code]
            works fine.

  4. Thanks Brad- This was very helpful!

    1. Brad Dalton Avatar
      Brad Dalton

      Good stuff!

  5. Hey nice Article. I got one question, how can I disable the featured images on the frontpage? Greetings from Germany

    1. Brad Dalton Avatar
      Brad Dalton

      You can use CSS or PHP code:
      [code]
      .home .post-image {
      display: none;
      }
      [/code]

  6. Omar Faruque Avatar
    Omar Faruque

    These code for general Excerpt Length
    [code]
    add_filter(‘excerpt_length’, ‘specific_category_excerpt_length’);
    function specific_category_excerpt_length($length) {

    return 300;

    }
    [/code]

  7. Hi Brad,

    Great post. I’m using eleven40 pro but I can’t figure out how to make ALL post listings featured.

    I’ve set “features” to 10 and “posts per page” to 10 which works great, but then when I click on the second or third pages of posts I get grid posts again.

    Do you know how to fix this? Any help is greatly appreciated! 🙂

    Kind regards,

    David

    1. Brad Dalton Avatar
      Brad Dalton

      Hi David

      I just tested this and it works.

      Simply remove the home.php file from your child themes folder and all archives will display as features.

  8. My homepage first post showing the full content. I want show it excerpts. Have you any idea? I am using Genesis and eleven 40 theme.

    1. Brad Dalton Avatar
      Brad Dalton

      Shemul

      Are you referring to eleven 40 or Eleven 40 pro?

  9. dave fogel Avatar
    dave fogel

    The read more link in your email doesn’t work.

    1. Brad Dalton Avatar
      Brad Dalton

      Yeah this has something to do with Jetpack as i know the sharing doesn’t work very well after the latest update also.

      I think we’ll see a patch come out for this soon.

      Thanks for letting me know dave.

    2. Brad Dalton Avatar
      Brad Dalton

      WordPress just sent me this email about the Jetpack bugs:

      Jeremy Herve wrote:

      Thanks for the report.

      We’re looking into it, and I’ll post again here as soon as I have some news.

      We just made a change to try to fix this issue. Could you try to publish a new post, and let me know if you still experience the same issue?
      Thanks!

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.