What Code Determines How WordPress Comments Behave?
WordPress will display comments in your theme based on whether comments have been submitted, providing it’s a single post or page, so long as your theme has the necessary comment template file present and providing you’ve got discussion options set so that comments are enabled.
The theme file that determines how comments are displayed is called:
comments.php
Comments.php is actually a template file that contains the database query logic for pulling comments out of the database as well as what details about the comments are to be displayed. This is then supplemented with CSS for styling and beautification.
But what’s all included in the comments.php template file? Read on….
Template Header for comments.php
Any template / theme files should begin with a template header to explain what the file is, does, etc – all the relevant details.
Section 1
It will specify the theme name, version details and not a lot else. Here’s an example from the WordPress Twentyseventeen Theme.
<?php
/**
* The template for displaying comments
*
* This is the template that displays the area of the page that contains both the current comments
* and the comment form.
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package WordPress
* @subpackage Twenty_Seventeen
* @since Twenty Seventeen 1.0
* @version 1.0
*/
Section 2
This is where checks are made to see if it’s a password protected post. If so, the routine bails and doesn’t continue displaying information.
Whatever the theme, the check should occur, and return if password protection is detected:
/*
* 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;
}
?>
Section 3
Finally, check to see if there actually are any comments to be made:
<div id="comments" class="comments-area">
<?php
// You can start editing here -- including this comment!
if ( have_comments() ) :
?>
Template Title for comments.php
The comment title is simply the intro to the fact you’ve got comments, the count of the comments or other visible heading above the comments, wherever they are displayed.
In the case of Twenty Seventeen, the comments number and the title above the comment is displayed:
<h2 class="comments-title">
<?php
$comments_number = get_comments_number();
if ( '1' === $comments_number ) {
/* translators: %s: Post title. */
printf( _x( 'One Reply to “%s”', 'comments title',
'twentyseventeen' ), get_the_title() );
} else {
printf(
/* translators: 1: Number of comments, 2: Post title. */
_nx(
'%1$s Reply to “%2$s”',
'%1$s Replies to “%2$s”',
$comments_number,
'comments title',
'twentyseventeen'
),
number_format_i18n( $comments_number ),
get_the_title());
}
?>
</h2>
Template WordPress Comments List for comments.php
Using the wp_list_comments function, the comment list is built as a list which includes:
- Avatar – sizing of author avatar
- List – list style – ordered or not
- Short Ping – varies based on pingback or not
- Reply Text – label surrounding replies
WordPress Comments displayed via the function, all in one neat little parcel of code:
<ol class="comment-list">
<?php
wp_list_comments(
array(
'avatar_size' => 100,
'style' => 'ol',
'short_ping' => true,
'reply_text' => twentyseventeen_get_svg(
array('icon' => 'mail-reply' )
) . __( 'Reply', 'twentyseventeen' ),
)
);
?>
</ol>
WordPress Comments Template Pagination for comments.php
There can be cases where you’ve got enough comments that they need to be paginated, thus adding navigation so that people can view them in chunks.
<?php
the_comments_pagination(
array(
'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) .
'<span class="screen-reader-text">' . __( 'Previous', 'twentyseventeen' ) . '</span>',
'next_text' => '<span class="screen-reader-text">' . __( 'Next','twentyseventeen' ) .
'</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
)
);
endif; // Check for have_comments().
Template WordPress Comments Closed Notice for comments.php
If you’ve got comments, but have closed them, for whatever reason, sometimes it would be nice to mention that fact to people, so that they don’t wonder exactly what’s going on.
// If comments are closed and there are comments, let's leave a little note, shall we?
if ( ! comments_open() && get_comments_number() && post_type_supports(
get_post_type(), 'comments' ) ) :
?>
<p class="no-comments"><?php _e( 'Comments are closed.', 'twentyseventeen'); ?></p>
<?php
endif;
Template Form and Finish for comments.php
The final stage is to display the comment form so that more comments can be added and then to close the routine, so that other code can continue executing elsewhere:
comment_form();
?>
</div> <!-- #comments -->
Conclusion
Have a WordPress website? Check out our Managed WordPress Hosting and see if we are a good fit for you. KnownHost offers 365 days a year, 24 hours a day, all 7 days of the week best in class technical support. A dedicated team ready to help you should you need our assistance. You’re not using KnownHost for the best webhosting experience? Well, why not?