Comment Template

WordPress displays comments in your theme based on the settings and code in the comments.php file within your WordPress theme.

Simple comments loop Simple comments loop

<?php
//Get only the approved comments 
$args = array(
    'status' => 'approve'
);

// The comment Query
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );

// Comment Loop
if ( $comments ) {
	foreach ( $comments as $comment ) {
		echo '<p>' . $comment->comment_content . '</p>';
	}
} else {
	echo 'No comments found.';
}
?>

The comments.php template contains all the logic needed to pull comments out of the database and display them in your theme.

Before we explore the template file you’ll want to know how to pull in the partial template file on the appropriate pages such as single.php. You’ll wrap the comment template tag in a conditional statement so comments.php is only pulled in if it makes sense to do.

// If comments are open or we have at least one comment, load up the comment template.
 if ( comments_open() || get_comments_number() ) :
     comments_template();
 endif;

functionality-comments-01

Top ↑

Another Comments.php Example Another Comments.php Example

Here’s an example of the comments.php template included with the Twenty Thirteen theme:

<?php
/**
 * The template for displaying Comments.
 *
 * The area of the page that contains comments and the comment form.
 *
 * @package WordPress
 * @subpackage Twenty_Thirteen
 * @since Twenty Thirteen 1.0
 */

/*
 * If the current post is protected by a password and the visitor has not yet
 * entered the password we will return early without loading the comments.
 */
if ( post_password_required() )
	return;
?>

<div id="comments" class="comments-area">

	<?php if ( have_comments() ) : ?>
		<h2 class="comments-title">
			<?php
				printf( _nx( 'One thought on "%2$s"', '%1$s thoughts on "%2$s"', get_comments_number(), 'comments title', 'twentythirteen' ),
					number_format_i18n( get_comments_number() ), '<span>' . get_the_title() . '</span>' );
			?>
		</h2>

		<ol class="comment-list">
			<?php
				wp_list_comments( array(
					'style'       => 'ol',
					'short_ping'  => true,
					'avatar_size' => 74,
				) );
			?>
		</ol><!-- .comment-list -->

		<?php
			// Are there comments to navigate through?
			if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
		?>
		<nav class="navigation comment-navigation" role="navigation">
			<h1 class="screen-reader-text section-heading"><?php _e( 'Comment navigation', 'twentythirteen' ); ?></h1>
			<div class="nav-previous"><?php previous_comments_link( __( '&larr; Older Comments', 'twentythirteen' ) ); ?></div>
			<div class="nav-next"><?php next_comments_link( __( 'Newer Comments &rarr;', 'twentythirteen' ) ); ?></div>
		</nav><!-- .comment-navigation -->
		<?php endif; // Check for comment navigation ?>

		<?php if ( ! comments_open() && get_comments_number() ) : ?>
		<p class="no-comments"><?php _e( 'Comments are closed.' , 'twentythirteen' ); ?></p>
		<?php endif; ?>

	<?php endif; // have_comments() ?>

	<?php comment_form(); ?>

</div><!-- #comments -->

Top ↑

Breaking down the comments.php Breaking down the comments.php

The above comments.php can be broken down to the below parts for better understanding.

  1. Template Header
  2. Comments Title
  3. Comment Listing
  4. Comment Pagination
  5. Comments are closed message.
  6. The End

Template Header Template Header

This template begins by identifying the template.

<?php
/**
 * The template for displaying Comments.
 *
 * The area of the page that contains comments and the comment form.
 *
 * @package WordPress
 * @subpackage Twenty_Thirteen
 * @since Twenty Thirteen 1.0
 */

Next, there’s a test to see if the post is password protected and, if so, it stops processing the template.

/*
 * If the current post is protected by a password and the visitor has not yet
 * entered the password we will return early without loading the comments.
 */
if ( post_password_required() )
	return;
?>

Finally, there’s a test to see if there are comments associated with this post.

<div id="comments" class="comments-area">

	<?php if ( have_comments() ) : ?>

Top ↑

Comments Title Comments Title

Prints out the header that appears above the comments.

Note: Uses the _nx() translation function so other developers can provide alternative language translations.

		<h2 class="comments-title">
			<?php
				printf( _nx( 'One thought on "%2$s"', '%1$s thoughts on "%2$s"', get_comments_number(), 'comments title', 'twentythirteen' ),
					number_format_i18n( get_comments_number() ), '<span>' . get_the_title() . '</span>' );
			?>
		</h2>

Top ↑

Comment Listing Comment Listing

The following snippet creates an ordered listing of comments using the wp_list_comments() function.

		<ol class="comment-list">
			<?php
				wp_list_comments( array(
					'style'       => 'ol',
					'short_ping'  => true,
					'avatar_size' => 74,
				) );
			?>
		</ol><!-- .comment-list -->

Top ↑

Comment Pagination Comment Pagination

Checks to see if there are enough comments to merit adding comment navigation and, if so, create comment navigation.

		<?php
			// Are there comments to navigate through?
			if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
		?>
		<nav class="navigation comment-navigation" role="navigation">
			<h1 class="screen-reader-text section-heading"><?php _e( 'Comment navigation', 'twentythirteen' ); ?></h1>
			<div class="nav-previous"><?php previous_comments_link( __( '&larr; Older Comments', 'twentythirteen' ) ); ?></div>
			<div class="nav-next"><?php next_comments_link( __( 'Newer Comments &rarr;', 'twentythirteen' ) ); ?></div>
		</nav><!-- .comment-navigation -->
		<?php endif; // Check for comment navigation ?>

Top ↑

Comments are closed message. Comments are closed message.

If comments aren’t open, displays a line indicating that they’re closed.

		<?php if ( ! comments_open() && get_comments_number() ) : ?>
		<p class="no-comments"><?php _e( 'Comments are closed.' , 'twentythirteen' ); ?></p>
		<?php endif; ?>

Top ↑

The End The End

This section ends the comments loop, includes the comment form, and closes the comment wrapper.

	<?php endif; // have_comments() ?>

	<?php comment_form(); ?>

</div><!-- #comments -->

Top ↑

Comments Pagination Comments Pagination

If you have a lot of comments (which makes your page long), then there are a number of potential benefits to paginating your comments. Pagination helps improve page load speed, especially on mobile devices.
Enabling comments pagination is done in two steps.

  1. Enable paged comments within WordPress by going to Settings > Discussion , and checking the box “Break comments into pages” . You can enter any number for the “top level comments per page”.
  2. Open your comments.php template file and add the following line where you want the comment pagination to appear.
<div class="pagination">
    <?php paginate_comments_links(); ?>
</div>

Top ↑

Alternative Comment Template Alternative Comment Template

On some occasions you may want display your comments differently within your theme. For this you would build an alternate file (ex. short-comments.php) and call it as follows:

 <?php comments_template( '/short-comments.php' ); ?> 

The path to the file used for an alternative comments template should be relative to the current theme root directory, and include any subfolders. So if the custom comments template is in a folder inside the theme, it may look like this when called:

<?php comments_template( '/custom-templates/alternative-comments.php' ); ?> 

Top ↑

Function Reference Function Reference

Top ↑

Functions reference for retrieving comments meta Functions reference for retrieving comments meta