Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to implement pagination on a category page. The pagination is working, but now for some reason the pagination plugin I'm using is noting one extra page of results, when that page is actually empty.

I'm pretty sure it doesn't have anything to do with the pagination plugin because I've seen reports of people having the same problem with next_posts_link and previous_posts_link.

Anybody have any idea how this could happen?

Loop:

<?php       
global $myOffset;
$myOffset = 11;
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('offset='.$myOffset.'&cat=6&posts_per_page=12'.'&paged='.$paged);
?>

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

STUFF

<?php endwhile; ?>

<?php if(function_exists('wp_paginate')) {
   // get yo paginate on
   wp_paginate();
} ?>

Edit: I am also using this functions.php plugin to enable the offset parameter in a custom wp_query:

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
share|improve this question
    
Are you able to try another pagination plugin just incase its that plugin not obeying the offset? Have you tried wordpress.org/extend/plugins/wp-pagenavi I swear by it but never needed to use it with an offset so I cannot say this will fix your issue but worth a try... –  Brady Apr 12 '11 at 14:42
add comment

2 Answers

up vote 3 down vote accepted

I am not sure, but I have a guess that it is because you are using pagination and offset at the same time. Pagination might be calculated for whole set, but you are reducing set size with offset so number of pages becomes overestimated.

share|improve this answer
1  
You are right, that's it! When offset is 0, every paginated page has contents (no empty pages). Now of course the tough question, is there any way around this? I'm using this function in conjunction with a filter to enable offset in a custom wp_query and still have pagination: 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 –  Squrler Apr 12 '11 at 13:29
    
@Squrler sorry, I hadn't worked with pagination enough to have a good idea how to work around it. I'll update if I think of something. btw please add this code snippet to original question so it's easier to read. –  Rarst Apr 12 '11 at 13:38
    
+1 - to moving the code into the question.. (code blocks are horrible to read in comments). –  t31os Apr 12 '11 at 14:08
    
Will do that. As an aside (and please, I'm sorry if I should not ask this here), is it possible to add code blocks in a comment? –  Squrler Apr 12 '11 at 14:28
1  
@Squrler not sure what you mean. You can add code to comments (as you did), but you can't indent it and such, comments are not really meant for that. –  Rarst Apr 12 '11 at 14:36
show 1 more comment

You could try to use the more native get_adjacent_posts() function to see if the problem is the next_posts_link() function. For additional information i wrote an educational plugin that's well documented/commented to giude you through the process.

share|improve this answer
    
Hey, thanks for replying. I'm not sure I understand what you mean with 'the more native function'. Isn't next_posts_link also a native function? In addition, I'm using the wp_paginate function and not the next_posts_link function. –  Squrler Apr 12 '11 at 12:34
    
@Squrler - there are a some functions inside wordpress that offer a layer above other functions. So next_posts_link() is just a simplified version of get_adjacent_posts(). There are many others like that. Another example has_post_format() is a layer above is_object_in_term(). Just take a look at the plugin code and read through the inline-comments and you'll understand better. –  kaiser Apr 12 '11 at 12:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.