Reputation: 424
In my admin I have 12 posts tagged with 'Efforts', though there are lots of other posts.
The 3 most recent posts tagged with 'Efforts' show up on the homepage, the rest show up in a page, not surprisingly titled "Efforts"
On that Efforts page I needed to have pagination with 5 posts per page but alos have it offset by 3.
On a blog post an can no longer seem to locate, the Following was suggested:
$temp = $wp_query;
$number_of_posts_per_page = 5;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$number_of_posts_past = $number_of_posts_per_page * ($paged - 1);
$off = 3 + (($paged > 1) ? $number_of_posts_past : 0);
wp_reset_query();
wp_reset_postdata();
$wp_query = new WP_Query('category_name=efforts&posts_per_page='.$number_of_posts_per_page.'&offset='.$off);
That works great except that there is one too many paginated pages and the last page is blank.
Why does the 'Previous Entries' link show up on page 2 and take you to a blank page.
Here's the full code for reference:
<?php
global $options;
foreach ($options as $value) {
if (get_settings( $value['id'] ) === FALSE) {
$$value['id'] = stripslashes( $value['std'] );
} else {
$$value['id'] = stripslashes( get_settings( $value['id'] ) );
}
}
?>
<?php get_header(); ?>
<?php /* Enables two or three columns */
if ($et_threecolumn_disable == "false") { ?> <?php include(TEMPLATEPATH."/sidebar.php");?><?php } ?>
<div class="content <?php if ($et_threecolumn_disable == "false") { ?> <?php echo $et_columnorder; ?> <?php } else { ?> content-two-column<?php echo $et_columnorder; ?> <?php } ?>">
<?php
$temp = $wp_query;
$number_of_posts_per_page = 5;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$number_of_posts_past = $number_of_posts_per_page * ($paged - 1);
$off = 3 + (($paged > 1) ? $number_of_posts_past : 0);
wp_reset_query();
wp_reset_postdata();
$wp_query = new WP_Query('category_name=efforts&posts_per_page='.$number_of_posts_per_page.'&offset='.$off);
if (have_posts()) : while (have_posts()) : the_post();
$video = get_post_custom_values('YT_video_id');
?>
<div class="post" id="post-<?php the_ID(); ?>">
<h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
<h2><?php the_time('F jS, Y') ?> | <?php the_category(', ') ?> | <?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?></h2>
<div class="entry">
<?php the_content('Read More'); ?>
<iframe id="player" type="text/html" width="600" height="355" src="http://www.youtube.com/embed/<?php echo $video[0] ?>" frameborder="0"></iframe>
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
</div>
<?php if (function_exists('the_tags')) { the_tags('<p class="postmetadata">Tags: ', ', ', '</p>'); } ?>
</div>
<?php endwhile; endif; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('← Previous Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next Entries →') ?></div>
</div>
<?php wp_reset_query(); $wp_query = null; $wp_query = $temp;?>
</div>
<?php include(TEMPLATEPATH."/primary-sidebar.php");?>
<?php get_footer(); ?>
Upvotes: 1
Views: 2178
Reputation: 424
I ran a few tests.
$temp = $wp_query;
$number_of_posts_per_page = 5;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$number_of_posts_past = $number_of_posts_per_page * ($paged - 1);
$off = 3 + (($paged > 1) ? $number_of_posts_past : 0);
wp_reset_query();
wp_reset_postdata();
$wp_query = new WP_Query('category_name=efforts&posts_per_page='.$number_of_posts_per_page.'&offset='.$off);
$wp_query->found_posts; // 12
So found_posts seems to be ignoring offset and posts_per_page. And since according to the codex max_num_pages is calculated as $found_posts / $posts_per_page it was coming out to 3 pages.
I needed to calculate my own max_pages to feed to the pagination links.
$temp = $wp_query;
$number_of_posts_per_page = 5;
$initial_offset = 3;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$number_of_posts_past = $number_of_posts_per_page * ($paged - 1);
$off = $initial_offset + (($paged > 1) ? $number_of_posts_past : 0);
wp_reset_query();
wp_reset_postdata();
$wp_query = new WP_Query('category_name=efforts&posts_per_page='.$number_of_posts_per_page.'&offset='.$off);
$max_pages = ceil(($wp_query->found_posts - $initial_offset) / $number_of_posts_per_page);
// snipped a bunch of post related code that worked fine
<div class="navigation">
<div class="alignleft"><?php next_posts_link('← Previous Entries', $max_pages) ?></div>
<div class="alignright"><?php previous_posts_link('Next Entries →', $max_pages) ?></div>
</div>
Upvotes: 1