Threading and Paging in WordPress 2.7
Mar 09, 2009 | English | By Hazel | 4 Comments | Leer en EspaolWordPress 2.7 integrates the possibility of adding conversations in the comments, also it allow us to page the comments in a very simple way.
By default this functionality is disabled, so in order to enable it you need to go to the Control Panel in the menu “Settings” in the option “Discussion” and activate the option “Threading” and “Paging”.
This action will be enough if the theme you are using right now for your blog supports these features, but if it doesn’t you need to make a few more changes that we will be talking about in this tutorial.
First you need to create a file called “comments.php” inside of your theme’s folder, this is the file that WordPress uses to manage the comments and is very probable that this file already exists in you theme’s folder, if so let’s edit the content.
Password protection
At the beginning of the file let’s see if the comments are protected with a password, in WordPress 2.7 there’s a function called post_password_required that makes the validation, so the code will be like this:
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Please do not load this page directly. Thanks!');
if ( post_password_required() ) {
echo 'This post is password protected. Enter the password to view comments.';
return;
}
The loop of the comments
The first thing you need to do is verify that the post has existing comments, the code to do that is:
<?php if ( have_comments() ) :
//Comments here
else :
if ('open' == $post-comment_status) :
// If comments are open, but there are no comments.
else:
// comments are closed
endif;
endif;
?>
You can display the number of comments that the post has, like this:
<h3 id="comments">
<?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”
</h3>
The next thing you need to do is to display the existing comments, in previous versions we would have to use a for loop and different functions to print the fields like name, Website, avatar, comment, etc. In this version it is easier, you just need to use the function “wp_list_comments” and it will display all the information contained in a list.
<ol class="commentlist"> <?php wp_list_comments(); ?> </ol>
When the comments are displayed we can see that they have different CSS classes:
• comment, trackback and pingback: this classes are used to distinguish the comment type.
• byuser: it will be added if the content belong to a registered user.
• bypostauthor: it is used in the comment of the post’s author.
• odd and even: these are classes that are added for the odd and even comments respectively.
• thread-odd, thread-even: these are classes that are applied to the answers of a comment; they distinguish the odd answers from even answers.
• The class depth-1 is added to the superior level, depth-2 to the next level and so on.
• avatar, avatar-32, Photo and avatar-default: these classes are used in the comment’s avatar, where 32 is the size of the avatar, which can vary.
• children: is added to the list that contains the answers that are given to a comment.
Paging
Immediately after displaying the comments we can add the paging with the functions “previous_comments_link” and “next_comments_link” like this:
<div class="navigation"> <div class="alignleft"><?php previous_comments_link() ?></div> <div class="alignright"><?php next_comments_link() ?></div> </div>
Form
What you need to do first is to include the necessary JavaScript, who uses the form; in this way WordPress will position the form in the right place when the user replies any previous comment. In order to include the necessary JavaScript you need to edit the file “header.php” and add the following lines before the function “wp_head”:
if ( is_singular() ) wp_enqueue_script( 'comment-reply' );
The next step is to add two hidden fields to the form, one will have the id of the post in which we are making the comment (comment_post_ID) and the other will have the id of the comment in which we are replying (comment_parent), to do this we just have to call the function “comment_id_field” like this:
< ?php comment_id_fields(); ?>
The “textarea”, where the user writes the comment, must have the id “comment” and it is required so the answers will work properly. Also it is required that the form must be inside of a div with the id “respond”, in this way when the user clicks over the link “reply” the form will be positioned in the right place and it will appear right under the comment.
Now, if the user has the JavaScript disabled and clicks over the link “reply” of a comment, then the page will reload and it will be positioned in the form, this happens because we created a div with the id “respond”. In this moment we can show the message “Leave a Reply” like this:
<h3><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h3>
The % is replaced by the name of the person who is responding, remember that this will only happen when the user has the JavaScript disabled.
Canceling the comment
A new function has been added to this version of WordPress, which allow us to cancel the answer of a comment. If the user has the JavaScript enabled then the form will be shown in its original place. To display it we need to do this:
<?php cancel_comment_reply_link(); ?>
Conclusions
These are the changes that you need to do so the theme you’re using can support the new functionalities of WordPress 2.7; as you can see it is easier to work with comments now. In the next part of this tutorial I will show how to customize the comments so you can use a personalized avatar or use other CSS classes, for now these changes will be enough to integrate the new functionalities.







this really a good theme for the Digg