I’ve implemented category-based numeric pagination in my WordPress theme:
What I’ve done so far is create a custom page template with a list of paginated excerpts. Once a user clicks on an excerpt to view the full article I’d also like to paginate the list of returned articles displayed.
It’s my understanding that this is achieved in the theme’s ‘single.php’, though I may be wrong.
The pagination works for my custom template, but when I try implementing the custom query within single.php I think the pagination attempts to find the next page within the article itself, which is not what I’m trying to achieve (because it doesn’t exist). I’d like to simply paginate though all the returned articles so the user may navigate next and previous from one article in this category to the next.
I’ve read a bit about the next/previous links, but I’d rather use something like this blog post by Boutros AbiChedid – I stole this idea for my custom page template, and it’s awesome!
I’ve made a derivative of this class in the theme’s ‘template.php’ as pagination for my custom theme and it works great, but I’m missing something when it comes to getting this to work through the returned single posts.
Should I give up and use another custom page template that displays 1 full article per page where I know my pagination works like a charm? I could do that, but I want to do it right. That’s the goal.
Here’s what I have so far in single.php:
get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php $wp_query = new WP_Query('category_name=news_and_updates&showposts=1&paged=' . $paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<?php get_template_part( 'content', 'single' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; ?>
<?php if (function_exists("pagination")) {
pagination($wp_query->max_num_pages);
} ?>
</div><!-- #content -->
</div><!-- #primary -->
I found this tutorial that explains how to tweak pagination by replacing the next/previous links with a ‘paginate_links’ object. I just can’t seem to get it to work.
I know that I’ll need to add something like this class from the twentyeleven theme to the custom theme’s ‘functions.php’, then customize it to use numeric pagination and call it either in single.php or single-content.php:
if ( ! function_exists( 'twentyeleven_content_nav' ) ) :
/**
* Display navigation to next/previous pages when applicable
*/
function twentyeleven_content_nav( $nav_id ) {
global $wp_query;
if ( $wp_query->max_num_pages > 1 ) : ?>
<nav id="<?php echo $nav_id; ?>">
<h3 class="assistive-text"><?php _e( 'Post navigation', 'twentyeleven' ); ?></h3>
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'twentyeleven' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?></div>
</nav><!-- #nav-above -->
<?php endif;
}
endif; // twentyeleven_content_nav
/**
* Return the URL for the first link found in the post content.
*
* @since Twenty Eleven 1.0
* @return string|bool URL or false when no link is present.
*/
function twentyeleven_url_grabber() {
if ( ! preg_match( '/<as[^>]*?href=['"](.+?)['"]/is', get_the_content(), $matches ) )
return false;
return esc_url_raw( $matches[1] );
}
I’ll keep editing this post until I figure this out, unless someone who’s done this before answers.
I’m not sure if I fully understand what you’re trying to achieve, so my apologies if this doesn’t help.
By the looks of it you want to nest a custom loop inside a page. If you just want to customise a regular list of posts, then you should be looking at archive.php, not single.php. Check out this hierarchy for details of the different template types. In the simplest form, archive pages are for display multiple posts (archive.php) and single pages are for displaying a single post/page (single.php).
For a custom loop inside a page, avoid using $wp_query, as this is used for the actual page you’re looking at and will drive
For pagination, I would agree with the tutorial and use paginate_links. With a bit of CSS, this should give you the effect you want.
So all in all, the query should look something like this in almost its simplest form.