Listing Comments

Listing comments is simple; it takes just one function. The comments template, however, is complicated. That's because it needs to check for a lot of different conditions: whether comments are allowed, whether they've been closed, whether there are any comments, whether the user is logged in, and so forth. Listing 6-19 shows the comments.php file from Simplex Flex, which is almost identical to the comments.php file in Twenty Ten.

Listing 6-19. The Simplex Flex comments.php template

<div id="comments">

<div class="nopassword"><?php _e( 'This post is password protected. Enter the password to view any comments.', 'simplex-flex' ); ?></div> </div><!-- .comments -->

<?php // Display the header for the comment section ?>

<?php if ( have_comments() ) : // if there are comments ?> <h3 id="comments-title"><?php comments_number(

sprintf(_('No Responses to %s', 'simplex-flex'), '<em>' . get_the_title() . '</em>'), sprintf(_('One Response to %s', 'simplex-flex'), '<em>' . get_the_title() . '</em>'), sprintf(_('%% Responses to %s', 'simplex-flex'), '<em>' . get_the_title() . '</em>')

<?php // Display navigation for paged comments, at the top of the list ?>

<?php if ( get_comment_pages_count() > 1 ) : // if there is more than one page... ?> <div class="navigation">

<div class="nav-previous"><?php previous_comments_link( _('&larr; Older Comments', 'simplex-

<div class="nav-next"><?php next_comments_link( _('Newer Comments &rarr;', 'simplex-flex') );

<ol class="commentlist">

<?php // Display navigation for paged comments, at the bottom of the list ?>

<?php if ( get_comment_pages_count() > 1 ) : // if there is more than one page... ?> <div class="navigation">

<div class="nav-previous"><?php previous_comments_link( _('&larr; Older Comments', 'simplex-

<div class="nav-next"><?php next_comments_link( _('Newer Comments &rarr;', 'simplex-flex') );

<?php else : // else there are no comments yet... ?>

<?php if ( comments_open() ) : // If comments are open, but there are no comments (nothing to do here) ?>

<p class="nocomments"><?php _e('Comments are closed.', 'simplex-flex'); ?></p>

<?php // Display the comment form ?> <?php comment_form(); ?> </div><!-- #comments -- >

There's a lot going on here, so I'll take it one piece at a time.

First, if the post is password protected and the reader has not already entered the password, you have to prevent them from seeing the comments. This if statement prints an error message to the reader and exits without printing any comments.

Once that's out of the way, you can begin a comment Loop. You do the usual check to see if there are any comments before you print anything, but if there are, you print a header containing the number of comments. This is done with PHP's sprintf() function, which has an odd syntax. You don't need to change this section, so I won't go into detail here, but you can always refer to the documentation at php.net/sprintf if necessary.

The next section checks to see if there's more than one page of comments. This depends entirely on the settings you chose back in Settings ^ Discussion. If you chose not to split comments into multiple pages, nothing will happen here. If you did, however, this section prints the page navigation. This block of code will be repeated below the comments list as well.

Finally, you display the comments themselves. Here, you see the wp_list_comments() function; however, as you saw earlier in this chapter, you can write your own comments loop if you wish.

Once you print the navigation again, you're done with the if (have_comments()) portion of this Loop, but you're not done yet! You could print something in the event that there are no comments yet, but in this case you've chosen to just leave the comment area blank. However, next you need to print a message if comments have been closed. This is the message your readers will see if you turned off comments on an individual post after allowing them for a while, or if you chose to automatically close comments on older posts.

Once you have taken care of all that, you can print the comment form. The comment_form() function prints out a standard form and performs all the necessary checks to see whether comments are open. You could instead use a hand-crafted form, as all themes did prior to version 3.0, but I recommend using the comment_form() tag with whatever styles you need to make its appearance match your theme.

0 0

Post a comment