Site logo

[Resolved] Change Number of Posts Displayed on Subsequent Pages

Home Forums Support [Resolved] Change Number of Posts Displayed on Subsequent Pages

Home Forums Support Change Number of Posts Displayed on Subsequent Pages

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2542570
    Dan

    Under Layout>blog – when selecting ‘Make first post featured’ and you use 2 columns it looks good for the first page, but the second page it is uneven at the bottom, because it is an odd number of posts.

    first page example: https://barcelonanavigator.com/category/culture/

    second page example: https://barcelonanavigator.com/category/culture/page/2/

    Thanks.

    #2542989
    David
    Staff
    Customer Support

    Hi there,

    to show one more post on the paged instances of the archive, add this PHP Snippet:

    add_action( 'pre_get_posts', 'sk_query_offset', 1 );
    function sk_query_offset( &$query ) {
            if ( is_admin() ) {
                return;
            }
    
    	// Before anything else, make sure this is the right query...
    	if ( ! ( is_main_query() ) ) {
    		return;
    	}
    
            // Don't do anything to Elements.
            if ( 'gp_elements' === $query->get( 'post_type' ) ) {
                return;
            }
    
    	// First, define your desired offset...
    	$offset = -1;
    
    	// Next, determine how many posts per page you want (we'll use WordPress's settings)
    	$ppp = get_option( 'posts_per_page' );
    
    	// Next, detect and handle pagination...
    	if ( $query->is_paged ) {
    
    		// Manually determine page query offset (offset + current page (minus one) x posts per page)
    		$page_offset = $offset + ( ( $query->query_vars['paged']-1 ) * $ppp );
    
    		// Apply adjust page offset
    		$query->set( 'offset', $page_offset );
    
    	}
    	else {
    
    		// This is the first page. Set a different number for posts per page
    		$query->set( 'posts_per_page', $offset + $ppp );
    
    	}
    }
    
    add_filter( 'found_posts', 'sk_adjust_offset_pagination', 1, 2 );
    function sk_adjust_offset_pagination( $found_posts, $query ) {
            if ( is_admin() ) {
                return;
            }
    
    	// Define our offset again...
    	$offset = -1;
    
    	// Ensure we're modifying the right query object...
    	if ( is_main_query() ) {
    		// Reduce WordPress's found_posts count by the offset...
    		return $found_posts - $offset;
    	}
    	return $found_posts;
    }
    
    #2543001
    Dan

    hi David, that didn’t work – now the number of posts is uneven on my homepage:

    https://barcelonanavigator.com/

    #2543187
    Ying
    Staff
    Customer Support

    Hi Dan,

    Try this snippet instead, set the posts per page to 6 at settings > reading, so the 1st page will only show 5 posts and the rest of the pages will show 6 posts.

    add_action( 'pre_get_posts', 'yh_query_offset', 1 );
    function yh_query_offset( &$query ) {
            if ( is_admin() ) {
                return;
            }
    
    	// Before anything else, make sure this is the right query...
    	if ( ! ( is_main_query() ) ) {
    		return;
    	}
    
            // Don't do anything to Elements.
            if ( 'gp_elements' === $query->get( 'post_type' ) ) {
                return;
            }
    
    	// First, define your desired offset...
    	$offset = -1;
    
    	// Next, determine how many posts per page you want (we'll use WordPress's settings)
    	$ppp = get_option( 'posts_per_page' );
    
    	// Next, detect and handle pagination...
    	if ( $query->is_paged ) {
    
    		// Manually determine page query offset (offset + current page (minus one) x posts per page)
    		$page_offset = $offset + ( ( $query->query_vars['paged']-1 ) * $ppp );
    
    		// Apply adjust page offset
    		$query->set( 'offset', $page_offset );
    
    	}
    	else {
    
    		// This is the first page. Set a different number for posts per page
    		$query->set( 'posts_per_page', $offset + $ppp );
    
    	}
    }
    
    add_filter( 'found_posts', 'sk_adjust_offset_pagination', 1, 2 );
    function sk_adjust_offset_pagination( $found_posts, $query ) {
            if ( is_admin() ) {
                return;
            }
    
    	// Define our offset again...
    	$offset = -1;
    	//get posts per page from WP settings
    	$ppp = get_option( 'posts_per_page' );
    	$real_page_numbers =ceil(($found_posts - $ppp - $offset) / $ppp) + 1;
    
    	// Ensure we're modifying the right query object...
    	if ( is_main_query() && ! is_paged() ) {
    		// Reduce WordPress's found_posts count by the offset...
    		return $real_page_numbers * ($ppp + $offset);
    	}
    	 return $real_page_numbers * $ppp;
    	
    }
    
    #2543227
    Dan

    brilliant, that worked.

    thanks Ying and David!

    #2543257
    Ying
    Staff
    Customer Support

    You are welcome 🙂

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.