-
Denise Ochoa
Hi,
I’ve followed some of the instructions to customize the archives for the post categories.I marked the first post as featured post, but I’ve noticed that there is one or two “post” missing on the last row of the page.
How can I correct that?
Thanks!
Denise -
Hi there,
you would need to adjust the posts per page to “even” out the columns just for the first or subsequent pages.
First in Settings > Reading set the Posts per Page to
11
This will fill the rows for your home page.Then add this PHP Snippet to your site, and it will reduced Page 2 onwards to
9
pages, so it evens out those rows too.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 = -2; // 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 = -2; //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; }
-
Denise Ochoa
Hi David!
Thanks, the first adjustment on setting did fix the first page. Now it looks complete.
However once I activated the code suggested, the query loops on the homepage are affected by showing the 9 posts and are “breaking” the original settings.
And the archive pages are back as they were before.I used the snippets plugin to add the code and selected run everywhere.
Let me know your thoughts,
Denise -
Which query loop on the front page is affected?
If you do not want the code to affect the front page, try changing this:
if ( is_admin() ) { return; }
to
if ( is_admin() || is_front_page() ) { return; }
Please note that there’re 2 pieces of this code, so you need to modify both.
-
Denise Ochoa
Hey Ying!
That does fixes the issue with the homepage.
But it does not address the “missing” posts on the last row of the category archives pages.Thanks!
Denise -
Is the snippet active right now? I’m asking because I’m seeing 11 posts on page 2.
It’s supposed to be 9 posts with the snippet on.
Let me know 🙂
-
Denise Ochoa
Hola!
It is active. Something might not be quite right.
Thanks!
Denise -
Try changing the offset value to
2
, instead of-2
.And set posts per page at settings >reading to
9
instead of11
. -
Denise Ochoa
Perfecto!
Working as intended now!One more thing :s
I noticed that a query loop used at the end of the blog template to show related posts also increase to 11, when originally was set to 3 posts.Can I prevent that?
Thanks a lot!
-
I noticed that a query loop used at the end of the blog template to show related posts also increase to 11, when originally was set to 3 posts.
What is the URL?
Let me know 🙂
-
Denise Ochoa
Done!
Thanks!
Denise -
I see, try change this condition
if ( is_admin() || is_front_page() ) { return; }
to
if ( is_admin() || is_front_page() || is_single() ) { return; }
Let me know if this helps!
- You must be logged in to reply to this topic.