How To Add Comment Navigation Links To Your WordPress Theme

If you theme doesn’t include comment navigation, you can add this code to your functions file and use your themes comment form hook to display the next and previous comments.

Themes like Genesis already include this functionality.

//* Comment Navigation
add_action( 'genesis_before_comments', function() {
	
	if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
	?>
	<nav class="navigation comment-navigation" role="navigation">
		<h2 class="screen-reader-text"><?php _e( 'Comment navigation', 'wpsites' ); ?></h2>
		<div class="nav-links">
			<?php
				if ( $prev_link = get_previous_comments_link( __( 'Older Comments', 'wpsites' ) ) ) :
					printf( '<div class="nav-previous">%s</div>', $prev_link );
				endif;

				if ( $next_link = get_next_comments_link( __( 'Newer Comments', 'wpsites' ) ) ) :
					printf( '<div class="nav-next">%s</div>', $next_link );
				endif;
			?>
		</div><!-- .nav-links -->
	</nav><!-- .comment-navigation -->
	<?php
endif;
});
[/code]

Replace the genesis_before_comments hook in the code above with your themes comment form hook.

Otherwise, you can create a template tag and add the tag directly to your comments.php template, before and after the wp_list_comments function.

[code]
<?php your_comment_nav(); ?>

Add the following code to your functions file.

if ( ! function_exists( ‘your_comment_nav’ ) ) :
function your_comment_nav() {

if ( get_comment_pages_count() > 1 && get_option( ‘page_comments’ ) ) :
?>




Comments

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.