- This topic has 5 replies, 3 voices, and was last updated 3 years, 2 months ago by
Ying.
-
AuthorPosts
-
February 22, 2023 at 7:02 am #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.
February 22, 2023 at 10:17 am #2542989David
StaffCustomer SupportHi 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; }February 22, 2023 at 10:24 am #2543001Dan
hi David, that didn’t work – now the number of posts is uneven on my homepage:
February 22, 2023 at 1:26 pm #2543187Ying
StaffCustomer SupportHi Dan,
Try this snippet instead, set the posts per page to
6at 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; }February 22, 2023 at 1:57 pm #2543227Dan
brilliant, that worked.
thanks Ying and David!
February 22, 2023 at 2:25 pm #2543257Ying
StaffCustomer SupportYou are welcome 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.