• If you view my site here, https://laniwa.net/ you see that I have the first post as a full post, while the rest are excerpts. It works well on the first page where there are 5 posts, but on the rest of the pages there is a “blank” space where it looks like a 6th post should be. Is it possible that on all pages but the first, I show 6 posts instead of 5?

    Here is the code I am using to accomplish what I have:

    <?php if (have_posts()) :
    $counter = 1;
    toolbox_content_nav( 'nav-above' );
    while (have_posts()) : the_post();
    if( $counter == 1 && !$wp_query->query_vars['paged']) { ?>
    
    <?php get_template_part( 'content', get_post_format() ); ?>
    
    <?php } else { ?>
    
    <?php get_template_part( 'content-excerpt', get_post_format() ); ?>
    <?php }
    $counter++;
    endwhile;
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Niwa

    (@laniwa)

    Update:

    So I found this code on a website:

    add_action( 'pre_get_posts',  'set_posts_per_page'  );
    function set_posts_per_page( $query ) {
    
      global $wp_the_query;
    
      if ( ( $query === $wp_the_query ) && ( $query->is_search() ) ) {
        $query->set( 'posts_per_page', 6 );
      }
      elseif ( ( $query === $wp_the_query ) && ( $query->is_archive() ) ) {
        $query->set( 'posts_per_page', 5 );
      }
      // Etc..
    
      return $query;
    }

    I used it on my site, and it works for the search page. I am just not sure how to write in “if not on the home page” portion.

    This is what I was able to come up with that works. In your theme’s functions.php put the following code

    function set_posts_per_page( $query ) {
    
    	if ( ! $query->is_main_query() ) return;
    
    	if ( ! $query->is_paged ) {
    		$query->set( 'posts_per_page', 6 );
    		return;
    	}
    
    	if ( $query->is_paged ) {
    
    		$query->set( 'posts_per_page', 5 );
    
    	}
    
    }
    
    add_action( 'pre_get_posts', 'set_posts_per_page' );

    The only caveat to this is that you may have to add more conditions to the first if statement if you find it affecting other areas of your site. For instance search would get set to 6 per page since it is technically not paged.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display 5 posts on front page, 6 on the rest’ is closed to new replies.