Leo Cavallini
Leo Cavallini

Reputation: 11

Wordpress query_posts omitting results on posts_per_page

let's see the facts on my Wordpress page:

Default configuration in settings > reading > shows at most: 12 posts

What I want

In page 1 I'm getting posts 1 to 9 (9 total). In page 2, posts 13 to 24 (12 total). Posts 10, 11 & 12 are getting omitted, dunno why.

My code before the loop:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

if ($paged == 1) { $numposts = 9; }
elseif ($paged > 1) { $numposts = 12; }

query_posts('posts_per_page='.$numposts.'&paged='.$paged);

Cheers!

Upvotes: 0

Views: 184

Answers (2)

Leo Cavallini
Leo Cavallini

Reputation: 11

Found the answer finally: http://wpdevelopertips.com/different-number-of-posts-in-pagination-pages/

I needed:

1st page: posts #1 to #9 (total of 9)

2nd page: posts #10 to 24 (total of 12)

2nd page: 12 posts and on...

So, before the loop:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

if ($paged == 1) { $numposts = 9; $theoffset = "";}
elseif ($paged > 1) { $numposts = 12; $theoffset = 9 + ($paged - 2) * 12;}

query_posts('showposts='.$numposts.'&paged='.$paged.'&offset='.$theoffset);

Note the numbers in theoffset variable are related to the number of posts I want for each specific page.

Upvotes: 1

I'll take a guess: For the first page, the parameters are

?posts_per_page=9&paged=1

For the second page:

?posts_per_page=12&paged=2

I'd guess that Wordpress uses something along the lines of the following (pseudocode) to figure out which posts to include in the page. (It's what I would do, anyway.)

first_on_page = posts_per_page * (paged - 1) + 1
last_on_page = first_on_page + posts_per_page - 1

The first 9-post page (your page 1) would contain posts 1-9. The second 12-post page (your page 2) would contain posts 13-24.

That's why the problem is happening. As for a fix, I'm no Wordpress guru, so I can only suggest using 12-post pages everywhere and putting the salutation in some other way.

Upvotes: 0

Related Questions