Archived topic
How to fix pagination - $wp_query->max_num_pages
9 replies · Started by Marco on April 24, 2019
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
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 :)
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' );
Out of curiously, what would you add to genesis_after_endwhile to fix this?
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 );
Just to confirm, where did you add the generate_after_endwhile hook? Would be happy to add that hook to the theme I think :)
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
I've added generate_after_loop in GP 2.3: https://github.com/tomusborne/generatepress/commit/924e41868400e20d1fb1159709b701bb43ecb355
Thanks!
great, thanks
No problem - thanks for the suggestion :)