- This topic has 9 replies, 2 voices, and was last updated 4 years, 11 months ago by Tom.
-
AuthorPosts
-
April 24, 2019 at 5:25 am #877430Marco
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
April 24, 2019 at 7:40 am #877728TomLead DeveloperLead DeveloperHi 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 π
April 24, 2019 at 9:04 am #877830Marcothis 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' );
April 24, 2019 at 4:06 pm #878271TomLead DeveloperLead DeveloperOut of curiously, what would you add to
genesis_after_endwhile
to fix this?April 24, 2019 at 4:13 pm #878279Marcohad 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 );
April 25, 2019 at 7:40 am #879249TomLead DeveloperLead DeveloperJust to confirm, where did you add the
generate_after_endwhile
hook? Would be happy to add that hook to the theme I think πApril 25, 2019 at 10:02 am #879432MarcoHi,
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
April 25, 2019 at 3:53 pm #879732TomLead DeveloperLead DeveloperI’ve added
generate_after_loop
in GP 2.3: https://github.com/tomusborne/generatepress/commit/924e41868400e20d1fb1159709b701bb43ecb355Thanks!
April 25, 2019 at 3:54 pm #879734Marcogreat, thanks
April 25, 2019 at 3:55 pm #879737TomLead DeveloperLead DeveloperNo problem – thanks for the suggestion π
-
AuthorPosts
- You must be logged in to reply to this topic.