I’m trying to display the Next/Prev navigation buttons on my page which displays all of the pages within my ‘events’ custom post-type. The issue is that the buttons are not even displaying, but I have no idea why not. I have had this working with my Blog page posts, but I wonder whether there is something different I need to do with a custom post-type, considering that it’s capability is set to ‘page’ instead of ‘post’.
Code:
<?php
/* Queue the first post, that way we know
* what date we're dealing with (if that is the case).
*
* We reset this later so we can run the loop
* properly with a call to rewind_posts().
*/
if ( have_posts() )
the_post();
?>
<h1>
<?php _e( 'Results', 'twentyten' ); ?>
</h1>
<div class="resultDetails">
<p>Displaying all of our Events:</p>
</div>
<ul>
<?php
$paged = get_query_var('paged');
$paged = ($paged) ? $paged : 1;
?>
<?php $loop = new WP_Query( array( 'post_type' => 'events', 'posts_per_page' => -1, 'paged' => $paged ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="resultItem">
<?php if (has_post_thumbnail( $post->ID )): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/thumbs.php?src=<?php echo $image[0]; ?>&w=120&h=120&zc=1" alt="<?php the_title(); ?>" /></a>
<?php endif; ?>
<div id="resultText">
<h2 class="redSubHeader"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</div>
</li>
<?php endwhile;?>
<nav class="navigation">
<ul>
<li class="next-posts post-nav"><?php next_posts_link('Next') ?></li>
<li class="prev-posts post-nav"><?php previous_posts_link('Prev') ?></li>
</ul>
</nav>
</ul>
Any help would be greatly appreciated!
Thanks
next_posts_link()
andprevious_posts_link()
use the global$wp_query->max_num_pages
to determine if there are more pages. since you’re using a new instance ofWP_Query
, the$wp_query
global does not relate to this new query.Try explicitly passing
$loop->max_num_pages
to the function:or if you’re not using the original query, call
query_posts()
with your arguments instead of creating a newWP_Query
instance.EDIT – also, as @kevin mentioned,
posts_per_page
set to -1 will load all posts, so you’d have to change that as well for it to paginate.