Skip to content

Instantly share code, notes, and snippets.

@cdils
Created April 11, 2014 14:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cdils/10473918 to your computer and use it in GitHub Desktop.
Save cdils/10473918 to your computer and use it in GitHub Desktop.
Add Previous/Next pagination to single posts within a category. If there are no adjacent posts, don't display anything.
<? php //remove this line
// Adding pagination before and after post entry in Genesis Framework
add_action( 'genesis_before_entry', 'cd_prev_next_posts_nav' );
add_action( 'genesis_after_entry', 'cd_prev_next_posts_nav' );
// Add Prev/Next post navigation within a single category
function cd_prev_next_posts_nav() {
// Check to see if there are previous and next posts
$previous = get_adjacent_post( true, '', true );
$next = get_adjacent_post( true, '', false );
//bail if we're not on a single post with a previous or next post available
if ( ! is_single() || ( empty( $next ) || empty( $previous ) ) ) {
return;
}
echo '<div class="archive-pagination">';
// build links to prev/next post in same category
$prev_link = previous_post_link( '<div class="pagination-previous alignleft">%link</div>', '&laquo; Previous Post', TRUE );
$next_link = next_post_link( '<div class="pagination-next alignright">%link</div>', 'Next Post &raquo;', TRUE );
echo '</div>';
}
@waynebcox
Copy link

Thanks for this. Wondering how you would change the code if you wanted to add pagination on a single post but not limit it to within a category?

Could you add simple pagination that would just go to the previous/next post?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment