[Resolved] How to fix pagination – $wp_query->max_num_pages

Home Forums Support [Resolved] How to fix pagination – $wp_query->max_num_pages

Home Forums Support How to fix pagination – $wp_query->max_num_pages

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #877430
    Marco

    Hello
    I need to fix the pagination somehow as it shows the wrong numbers – on my homepage/blogpage I show 1 featured and six regular posts in a two column layout – on the following pages, the layout is changed to a 3-column layout and showing 9 posts – this obviously messes up the pagination – Genesis uses a genesis_after_endwhile hook I could use but I don’t see how I could fix the pagination issue using Generatepress – hope somebody can shed some light.

    thanks

    GeneratePress 2.2.2
    #877728
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    How are you changing the loop to show a different number of posts as of right now? Can you share your code?

    Let me know πŸ™‚

    #877830
    Marco

    this is what I have so far:

    function themeprefix_change_posts_per_page( &$query ) {
    	
        if ( is_admin() || ! $query->is_main_query() || ! function_exists( 'generate_blog_get_defaults' ) )
            return $query;
    	
    	// get GeneratePress blog defaults
    	$settings = wp_parse_args(get_option('generate_blog_settings', array()), generate_blog_get_defaults());
    	
    	// only proceed if "featured_column"
    	if ( $settings['featured_column'] ) {
    		
    		// 1st page
    		if ( ! $query->is_paged )
    			return $query;
    		
    		$page = $query->query_vars['paged'];
    		
    		$nums = 9;
    		$diff = get_option( 'posts_per_page' ) - $nums;	
    		//$offs = ( $query->query_vars['paged'] - 1 ) * $nums + $diff;	// offset to correct for diference
    		$offs = ( $page - 1 ) * $nums + $diff;	// offset to correct for diference
    		
    		$query->set( 'posts_per_page', $nums );
    		$query->set( 'offset', $offs );
    		
    	}
    	
        return $query;
    };
    add_action( 'pre_get_posts', 'themeprefix_change_posts_per_page', 1 );
    
    function themeprefix_set_column_count() {
    	
    	// get GeneratePress blog defaults
    	$settings = wp_parse_args(get_option('generate_blog_settings', array()), generate_blog_get_defaults());
    	$count    = $settings['columns'];
    	
    	if ( ( is_home() || is_archive() ) && get_query_var( 'paged' ) > 0 )
    		$count =  floor(100 / 3);
    
        return $count;
    }
    add_filter( 'generate_blog_get_column_count', 'themeprefix_set_column_count' );
    #878271
    Tom
    Lead Developer
    Lead Developer

    Out of curiously, what would you add to genesis_after_endwhile to fix this?

    #878279
    Marco

    had to change the archive and index files so it would take that after_endwhile code – if changing the number of posts after the first page to e.g. 6 posts on the first and 9 posts on the following pages, the pagination needs to be fixed:

    the following code seems to work:

    unction themeprefix_fix_posts_nav() {
    	
    	global $wp_query;
    	
    	$posts_on_1st = get_option( 'posts_per_page' );	// number of posts on 1st page
    	$posts_on_2nd = 9;								// number of posts on 2nd page and the following pages
    	
    	//echo 'posts: ' . $wp_query->found_posts;
    	
    	$max = ceil ( ( $wp_query->found_posts - $posts_on_1st ) / $posts_on_2nd ) + 1;
    		
    	$wp_query->max_num_pages = $max;
    }
    add_filter( 'generate_after_endwhile', 'themeprefix_fix_posts_nav', 5 );
    #879249
    Tom
    Lead Developer
    Lead Developer

    Just to confirm, where did you add the generate_after_endwhile hook? Would be happy to add that hook to the theme I think πŸ™‚

    #879432
    Marco

    Hi,
    just after the endwhile; and before generate_content_nav( ‘nav-below’ ); like: do_action( ‘generate_after_endwhile’ );

    only added it in the index.php and archive.php file so far

    #879732
    Tom
    Lead Developer
    Lead Developer
    #879734
    Marco

    great, thanks

    #879737
    Tom
    Lead Developer
    Lead Developer

    No problem – thanks for the suggestion πŸ™‚

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