Site logo

[Resolved] 9 blog items on home, 10 on the next pages

Home Forums Support [Resolved] 9 blog items on home, 10 on the next pages

Home Forums Support 9 blog items on home, 10 on the next pages

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #2423343
    jmarc

    Hello,

    I made a new post from an old one created by Joe ( https://generatepress.com/forums/topic/9-blog-items-on-home-10-on-following-pages/#post-2423342 )

    Is it possible to get 9 blog posts on the homepage and 10 on the following pages? It seems a bit odd when using the standard setup with the big featured post on the homepage.

    So I use the code given by David for my site but pagination is not working fine for the last page (I have one or two pages more and when cliking I have an Error 404)
    Can you please take a look ?
    Thank you very much

    JMarc

    #2424514
    Ying
    Staff
    Customer Support

    Hi there,

    Can you try this Snippet instead:

    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;
            }
    
    	else {
    	// 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 ) {
          
    	// 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() && ! is_admin()) {
    		// Reduce WordPress's found_posts count by the offset...
    		return $real_page_numbers * ($ppp + $offset);
    	}
    	 return $real_page_numbers * $ppp;	
    }
    
    #2424863
    jmarc

    Hello Ying

    It works fine !
    Thank you very much

    Regards

    #2425966
    Ying
    Staff
    Customer Support

    You are welcome 🙂

    #2426955
    jmarc

    ..

    #2427706
    Ying
    Staff
    Customer Support
    #2427904
    jmarc

    Hi Ying
    got it thanks 🙂
    JM

    PS : double authentification to get into this forum is a real Pain in the a**

    #2428087
    Ying
    Staff
    Customer Support

    Lol…I have to do the same thing every day to start my work…

    #2446884
    jmarc

    Hi Ying,

    since I’ve put this code, pagination in admin doesn’t work anymore….
    For instance, at the end of the page …/wp-admin/edit.php there is no pagination !
    Can you please help ?

    Thank you
    JM

    #2446976
    David
    Staff
    Customer Support

    Hi there,

    edit this line:

    function sk_query_offset( &$query ) {

    And change it to:

    function sk_query_offset( $query ) {

    ie. remove the & ampersand.

    #2447609
    jmarc

    Hi David

    thank you but that doesn’t fix it …

    JM

    #2448340
    Ying
    Staff
    Customer Support

    I just tested the code, it seems working fine.

    Can you link us to your site? And can you copy and paste the exact code you are using and wrap it with the CODE tags so we can see if there’s any error in the code?

    Let me know!

    #2448345
    jmarc

    Hi Ying

    here’s the exact code I use :

    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;
    	//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;
    	
    }
    

    Thank you

    #2448347
    jmarc

    it seems that .tablenav-pages do not appear (at the bottom of ../wp-admin/edit.php)

    #2448363
    Ying
    Staff
    Customer Support

    I can not see the admin dashboard though, it will require an admin login.

    But I can replicate the issue on my end. I’ll see how to modify the code 🙂

    Will get back to you later.

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