Blog

ExtJS and Sencha Touch Themes and Templates

Threading and Paging in WordPress 2.7

Mar 09, 2009 | English | By | 4 Comments | Leer en Español

WordPress 2.7 integrates the possibility of adding conversations in the comments, also it allow us to page the comments in a very simple way.

Threading and Paging in WordPress 2.7
Author: Crysfel

I'm a software developer with 6+ years of experience, when I'm not developing software I may be writing a tutorial, you can follow me on twitter

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 &#8220;<?php the_title(); ?>&#8221;
</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.

4 Responses to “Threading and Paging in WordPress 2.7”

  • Funny Jun 13, 2009

    this really a good theme for the Digg

  • Tnelson Sep 30, 2009

    Hey very nice blog!!….I’m an instant fan, I have bookmarked you and I’ll be checking back on a regular….See ya

  • Marc Shaw Oct 15, 2009

    Hey, I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say GREAT blog!…..I”ll be checking in on a regularly now….Keep up the good work! :)

    - Marc Shaw

  • Bob74 Oct 22, 2009

    But it’s not clear that people understand what it means. ,

Leave a Reply







Updates

RSS

Subscribe to our feeds to receive updates of our newest posts and free tutorials.

Site search

Maybe we have what you need, would you like to search first?

Donations

Would you buy me a cup of coffee? I am sharing my knowledge and time with you, help this project grow. Thank you!