Query_posts, offset and pagination
-
Ok, this is the situation:
In the home page of my theme (magazeen) I have 3 groups of posts:
- Complete article (3 posts)
- Part of the articles (6 posts in 2 rows)
- Only titles (6 posts)
(Note1: Actually the distribution is different from the original theme: 2, 4 and 5, respectively, but I’m using different values. Anyway -I think- that difference shouldn’t change the core of the solution)
(Note2: All of them are from any category; basically 15 posts on the front page styled in three different ways*; like only one query instead of three different ones who are giving me the headache)
The theme works ok until you try to go to the older posts. If I put a:
<div id="navigation"> <div class="alignleft"><?php next_posts_link('« Older Entries') ?></div> <div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div> </div><!-- End navigation -->
It takes me to a “second page” (domain.com/page/2) that looks exactly the same as the first one.
From what I’ve been reading I understand that pagination doesn’t work well with
query_posts
so I went to check the theme and:query_posts( 'showposts=3' );
query_posts( 'showposts=6&offset=3' );
query_posts( 'showposts=6&offset=9' );
I searched again if there were a workaround and I found this post from Web Blogs Tool Collection. The solution consisted in adding some variables:
<?php $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query('showposts=3'.'&paged='.$paged); ?> <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?> <!-- Stuff --> <?php endwhile; ?> <!-- Navigation --> <?php $wp_query = null; $wp_query = $temp; ?>
It worked, but only for the first part (the one without
offset
). The second and the third one acted like nothing happened.So, “page 1” showed me something like:
[1° Group]
- Post 1
- Post 2
- Post 3
[2° Group]
- Post 4 | Post 5
- Post 6 | Post 7
- Post 8 | Post 9
[3° Group]
- Post 10
- Post 11
- Post 12
- Post 13
- Post 14
- Post 15
If I clicked on the “Older posts” (second page) it showed:
[1° Group]
- Post 4
- Post 5
- Post 6
[2° Group]
- Post 4 | Post 5
- Post 6 | Post 7
- Post 8 | Post 9
[3° Group]
- Post 10
- Post 11
- Post 12
- Post 13
- Post 14
- Post 15
I continued my investigation and I found that pagination and offset won’t work together and again, I found a workaround in the same blog. The idea first is to add a new functin in the
functions.php
of the theme:function my_post_limit($limit) { global $paged, $myOffset; if (empty($paged)) { $paged = 1; } $postperpage = intval(get_option('posts_per_page')); $pgstrt = ((intval($paged) -1) * $postperpage) + $myOffset . ', '; $limit = 'LIMIT '.$pgstrt.$postperpage; return $limit; } //end function my_post_limit}
Then add a filter reference…
<?php add_filter('post_limits', 'my_post_limit'); ?>
Adding two lines to the previous code…
<?php <strong>global $myOffset;</strong> <strong>$myOffset = 1;</strong> $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); <strong>$wp_query->query('offset='.$myOffset.'&showposts=5'.'&paged='.$paged);</strong> ?>
And finally…
<?php $wp_query = null; $wp_query = $temp; ?>
<?php remove_filter(‘post_limits’, ‘my_post_limit’); ?>`Adding that hack, now the “first page” looked like this:
(Settings > Reading > Blog pages show at most 10 posts)
[1° Group]
- Post 1
- Post 2
- Post 3
[2° Group]
- Post 4 | Post 5
- Post 6 | Post 7
- Post 8 | Post 9
- Post 10 | Post 11
- Post 12 | Post 13
(Settings > Reading > Blog pages show at most 6 posts)
[2° Group]- Post 4 | Post 5
- Post 6 | Post 7
- Post 8 | Post 9
(I didn’t include the 3° group, because I didn’t change it. I’m trying to solve this with two groups first)
The second page:
[1° Group]
- Post 4
- Post 5
- Post 6
(Settings > Reading > Blog pages show at most 10 posts)
[2° Group]- Post 14 | Post 15
- Post 16 | Post 17
- Post 18 | Post 19
- Post 20 | Post 21
- Post 22 | Post 23
Okay, it makes sense, it’s continuing the first page which ended on Post 13, but where are Post [7-13]?. The idea was that the [2° Group] continued starting from Post 7.
(Settings > Reading > Blog pages show at most 6 posts)
[2° Group]- Post 10 | Post 11
- Post 12 | Post 13
- Post 14 | Post 15
Again, Posts between 7 and 10 are lost.
Since the nature of the theme, I imagine the best to go to ‘older pages’ is to move the posts from lower groups (less info) to the upper groups (more info). Even, just going directly from the last post on the first page (Post 15) to start with the next one (16) on the second page would be ok.
I’ve been reading a lot I can’t find a solution, so any kind of help would be greatly appreciated!
* If you can come with a solution to do that, is welcome too. It seems to me that that way it should look cleaner
- The topic ‘Query_posts, offset and pagination’ is closed to new replies.