• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Change Number Of Posts On Homepage Only

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Change Number Of Posts On Homepage Only

This topic is: resolved
  • This topic has 8 replies, 5 voices, and was last updated 11 years ago by chad1008.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • May 8, 2015 at 9:24 pm #151089
    chad1008
    Member

    Fought with this one all day, and I've gotten pretty close - just need a fresh pair of eyes to tell me what I'm missing.

    I need my front page to display 3 posts, and all other pages (/page/2, etc.) to display a different number. Probably ten, but I'm testing with 2 as I don't have a huge database on my test site. The Blog Page template doesn't seem to allow for different post per page values.

    Here's the code I've been playing with... The offsets all work fine, but for some reason every page is grabbing the homepage post count. The variable works fine and modifies the query, but it does it for every page.

    add_action( 'pre_get_posts', 'w2wp_query_mod' );
    function w2wp_query_mod( $query ) {
    //Set up post count and pagination values
      global $paged;
      $homepage_count = 3;
      $otherpage_count = 2;
      $paged = $paged ? $paged : 1;
    
    //Use homepage post count on first page with no offset
    if ( $paged == 1 ) {
      $posts_per_page = $homepage_count;
      $offset = 0;
    
    // Use other pages post count on second page, offset by homepage post count
    } elseif ( $paged == 2 ) {
      $posts_per_page = $otherpage_count;
      $offset = $homepage_count;
    
    // use other page post count on all other pages. Offset by number of posts displayed on the homepage, plus all other pages before the current.
    } else {
      $posts_per_page = $otherpage_count;
      $offset = $homepage_count + ( $otherpage_count * ( $paged - 2 ) );
    }
     
    //Modify the query using the populated variables 
      if ( is_home() && $query->is_main_query() && !is_admin() ) {
    		$query->set( 'posts_per_page', $posts_per_page );
    		$query->set( 'post_type', array( 'post', 'w2wp_video' ) );
    		$query->set( 'offset', $offset );
        
    		return $query;
        }
    }

    The $paged values seem to work - I set up another function to output the current $paged value from the query, as well as the current $posts_per_page value from the above function - but that's always showing the $homepage_count value.

    I've added this into a fresh, unmodified copy of Genesis Child, just to have a clean slate and I get the same result.

    Any ideas? Hoping it's something painfully obvious that I've just overlooked!

    May 9, 2015 at 4:39 am #151194
    Victor Font
    Moderator

    I think you're over complicating things:

    function custom_posts_per_page($query) {
        if (is_home()) {
            $query->set('posts_per_page',3);
        }
    }  
    add_action('pre_get_posts', 'custom_posts_per_page');

    If you're not on the home page, the number of posts per page will be the value set in Admin Menu/Reading.


    Regards,

    Victor
    https://victorfont.com/
    Call us toll free: 844-VIC-FONT (842-3668)
    Have you requested your free website audit yet?

    May 9, 2015 at 6:38 am #151210
    upthink
    Participant

    I don't this will work. When paginating the pages are all "home" - so whether you are on http://abc.com/page/2 or http://abc.com the call to is_home() will return true

    May 9, 2015 at 6:50 am #151220
    Genesis Developer
    Member

    it will not work. You will get wrong pagination number on home page. You need to re-create a new function of genesis pagination function.


    Download Genesis Featured Posts Combo Widget | Simple Grid Layouts Plugin for Posts, CPTs and terms
    You can request new tips/help.

    May 9, 2015 at 7:10 am #151224
    Susan
    Moderator

    Have you tried the Genesis Grid Loop plugin? I have it on my test site (here), and am able to get a different number of posts on my home page, versus other pages

    May 9, 2015 at 9:29 am #151242
    chad1008
    Member

    @Susuan That's good solution, but I think I'm too stubborn to give up on coding it myself 😉


    @Genesis
    Developer

    Okay, let me back up a step then. Before the query modification I posted above, I was working with this custom loop function:

    remove_action ('genesis_loop', 'genesis_do_loop');
    add_action( 'genesis_loop', 'w2wp_custom_loop' );
    
    function w2wp_custom_loop() {
    //Set up post count and pagination values
      global $paged;
      $homepage_count = 3;
      $otherpage_count = 2;
      $paged = $paged ? $paged : 1;
    
    //Use homepage post count on first page with no offset
    if ( $paged == 1 ) {
      $posts_per_page = $homepage_count;
      $offset = 0;
    
    // Use other pages post count on second page, offset by homepage post count
    } elseif ( $paged == 2 ) {
      $posts_per_page = $otherpage_count;
      $offset = $homepage_count;
    
    // use other page post count on all other pages. Offset by number of posts displayed on the homepage, plus all other pages before the current.
    } else {
      $posts_per_page = $otherpage_count;
      $offset = $homepage_count + ( $otherpage_count * ( $paged - 2 ) );
    }
    
          global $query_args; // grab the current wp_query() args
          $args = array(
              'posts_per_page' => $posts_per_page,
              'offset' => $offset, 
              'paged'            => $paged, // respect pagination
              'post_type' => array( 'post', 'w2wp_video' )
          );
       
          genesis_custom_loop( wp_parse_args($query_args, $args) );
       
    }

    The pagination and offsets all work! The only problem is that I get a 404 message displaying after the posts on any page that shouldn't exist based on the post count value in Reading Settings. Any suggestions?

    May 9, 2015 at 9:55 am #151245
    Susan
    Moderator

    @chad1008 - I understand! I know some people don't like to use extra plugins, but others are just wanting a quick fix. 🙂

    May 9, 2015 at 12:06 pm #151286
    Genesis Developer
    Member

    You should clean your custom code ( keep a backup first ) and then add the following code in functions.php file

    $first_page_limit = 5;
    $posts_per_page = $next_pages_limit = 10;
            
    //add_filter( 'pre_get_posts', 'mwm_posts_per_page_archive_query' );
    function mwm_posts_per_page_archive_query( $query ) {
    
        if ( is_admin() || is_feed() || ! $query->is_main_query() ) {
            return $query;
        }
    
        global $first_page_limit, $next_pages_limit, $posts_per_page;
        $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
        
        if ( is_home() ) {
    
            if ( $paged == 1 ) {
                $posts_per_page = $first_page_limit;
            } else {
               $offset = $first_page_limit + (($paged - 2) * $next_pages_limit);
                set_query_var('offset', $offset);
            }
            set_query_var('posts_per_archive_page', $posts_per_page);
            set_query_var('posts_per_page', $posts_per_page);
            
            remove_action( 'genesis_after_endwhile', 'genesis_posts_nav' ); 
            add_action( 'genesis_after_endwhile', 'gd_custom_posts_nav' );
        }
    
        return $query;  
    }
    
    function gd_custom_posts_nav(){
      
      if ( 'numeric' === genesis_get_option( 'posts_nav' ) )
    		gd_numeric_posts_nav();
    	else
    		genesis_prev_next_posts_nav();
    }
    
    function gd_numeric_posts_nav() {
    
    	if( is_singular() )
    		return;
    
    	global $wp_query, $first_page_limit, $next_pages_limit;
    
    	//* Stop execution if there's only 1 page
    	if( $wp_query->max_num_pages <= 1 )
    		return;
        
      $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    	
      $found_posts = $wp_query->found_posts - $first_page_limit;
      $max = $wp_query->max_num_pages = intval( ceil( $found_posts / $next_pages_limit ) ) + 1;
    
    	//* Add current page to the array
    	if ( $paged >= 1 )
    		$links[] = $paged;
    
    	//* Add the pages around the current page to the array
    	if ( $paged >= 3 ) {
    		$links[] = $paged - 1;
    		$links[] = $paged - 2;
    	}
    
    	if ( ( $paged + 2 ) <= $max ) {
    		$links[] = $paged + 2;
    		$links[] = $paged + 1;
    	}
    
    	genesis_markup( array(
    		'html5'   => '<div %s>',
    		'xhtml'   => '<div class="navigation">',
    		'context' => 'archive-pagination',
    	) );
    
    	echo '<ul>';
    
    	//* Previous Post Link
    	if ( get_previous_posts_link() )
    		printf( '<li class="pagination-previous">%s</li>' . "\n", get_previous_posts_link( apply_filters( 'genesis_prev_link_text', '«' . __( 'Previous Page', 'genesis' ) ) ) );
    
    	//* Link to first page, plus ellipses if necessary
    	if ( ! in_array( 1, $links ) ) {
    
    		$class = 1 == $paged ? ' class="active"' : '';
    
    		printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
    
    		if ( ! in_array( 2, $links ) )
    			echo '<li class="pagination-omission">…</li>';
    
    	}
    
    	//* Link to current page, plus 2 pages in either direction if necessary
    	sort( $links );
    	foreach ( (array) $links as $link ) {
    		$class = $paged == $link ? ' class="active"' : '';
    		printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
    	}
    
    	//* Link to last page, plus ellipses if necessary
    	if ( ! in_array( $max, $links ) ) {
    
    		if ( ! in_array( $max - 1, $links ) )
    			echo '<li class="pagination-omission">…</li>' . "\n";
    
    		$class = $paged == $max ? ' class="active"' : '';
    		printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
    
    	}
    
    	//* Next Post Link
    	if ( get_next_posts_link() )
    		printf( '<li class="pagination-next">%s</li>' . "\n", get_next_posts_link( apply_filters( 'genesis_next_link_text', __( 'Next Page', 'genesis' ) . '»' ) ) );
    
    	echo '</ul></div>' . "\n";
    
    }

    Download Genesis Featured Posts Combo Widget | Simple Grid Layouts Plugin for Posts, CPTs and terms
    You can request new tips/help.

    May 9, 2015 at 1:16 pm #151295
    chad1008
    Member

    Unreal.

    That's awesome - even solves the problem of the phantom navigation links that I was going to have to tackle next.

    Just saved me a ton of time working all of that out... Thanks a MILLION.

  • Author
    Posts
Viewing 9 posts - 1 through 9 (of 9 total)
  • The topic ‘Change Number Of Posts On Homepage Only’ is closed to new replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble