If you’re designing and coding themes for WordPress, please, I beg you, for love of all that is digitized and Tron like, separate your trackbacks and pingbacks from your comment threads. There is nothing more frustrating than having to navigate through a horde of unholy spam links to see the next comment in a thread. I also personally believe(ie I have no proof whatsoever) that a comment threat mucked with trackbacks correlates directly with less commenting. But trackbacks aren’t all bad, some people (not me) use them to find out more about the blog post, theme, story, whatever…so I wouldn’t just toss them aside like fruit cake.
If you look at most new well thought out themes, trackbacks are separated into another tab, or somehow minimized/hidden from the user that doesn’t want them. WordPress 2.7 changed the commenting code quite a bit, and now separating everything is even easier. Add a little jQuery magic and you can hide and show those trackbacks on demand. Enough with this banter, lets rock out with our text-editor out.
First a little catchup
So things are a wee bit different in the ol’ comment file in Wordpress 2.7. I highly suggest taking a peek at Otto’s WordPress 2.7 Comments Enhancements if you haven’t already. He outlines how the new comment loop works, and all the spiffy javascript for threaded comments.
Once you got a grasp on the comment changes in 2.7, checkout Matt’s article on separating pings from comments. I’m not going to go into too much detail so if you find yourself not understanding a part of the code, try checking out those articles.
Separation makes the heart grow fonder of pings
I think that is the saying. Lets start with the comment loop, first the whole thing,
if ( have_comments() ) : ?>
<h3 class="comment_title"><?php comments_number('No Comments', 'One Comment', '% Comments' );?></h3>
<ol class="commentlist" id="singlecomments">
<?php wp_list_comments(array('avatar_size'=>48, 'reply_text'=>'Reply', 'type'=>'comment')); ?>
</ol>
<h3 class="trackback_title">Trackbacks / Pingbacks</h3>
<a href="#pings" class="show_trackbacks">show trackbacks</a>
<ol id="pings" class="trackback">
<?php wp_list_comments('type=pings'); ?>
</ol>
<div class="comments-navigation">
<div class="alignleft"><?php previous_comments_link() ?></div>
<div class="alignright"><?php next_comments_link() ?></div>
</div>
<?php else : // this is displayed if there are no comments so far ?>
<?php if ('open' == $post->comment_status) :
// If comments are open, but there are no comments.
else :
// comments are closed
endif;
endif;
There is the comment loop with the trackbacks already separated. The only trick to the separation is setting the ‘type‘ option for the wp_list_comments function. Set the ‘type’ to ‘comment’, for comments, and pings for …well pings(trackbacks). Simple enough right? Let’s take a closer look at the HTML.
<h3 class="comment_title"><?php comments_number('No Comments', 'One Comment', '% Comments' );?></h3>
<ol class="commentlist" id="singlecomments">
<?php wp_list_comments(array('avatar_size'=>48, 'reply_text'=>'Reply', 'type'=>'comment')); ?>
</ol>
<h3 class="trackback_title">Trackbacks / Pingbacks</h3>
<a href="#pings" class="show_trackbacks">show trackbacks</a>
<ol id="pings" class="trackback">
<?php wp_list_comments('type=pings'); ?>
</ol>
This is really the meat of the code, you can see the two blocks of code, one for comments and one for pings. The only real differences are setting the ‘type’ for wp_list_comments and changing the id and class names for styling. There is also one addition for the pings, a link to show them(it has a class of ’show_trackbacks’). You can either put this link in the actual code, or inject it using javascript.
The jQuery
There’s a few ways to do this but I’m going to go with a simple slide up and slide down. First we’ll hide the trackbacks by calling slideUp() then add a function to detect the click on our show_trackbacks link which will call slideDown().
$('ol.trackback').slideUp();
$('.show_trackbacks').click(function(){
$('ol.trackback').slideDown();
});
That’s it?? Yeah, pretty simple actually, so I’ll leave you with something else useful. A full comment file ready for wordpress 2.7.
The whole thing
<?php // Do not delete these lines
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Please do not load this page directly. Thanks!');
if ( post_password_required() ) { ?>
<p class="nocomments">This post is password protected. Enter the password to view comments.</p>
<?php
return;
}
// add a microid to all the comments
function comment_add_microid($classes) {
$c_email=get_comment_author_email();
$c_url=get_comment_author_url();
if (!empty($c_email) && !empty($c_url)) {
$microid = 'microid-mailto+http:sha1:' . sha1(sha1('mailto:'.$c_email).sha1($c_url));
$classes[] = $microid;
}
return $classes;
}
add_filter('comment_class','comment_add_microid');
//Change date format
function change_comment_date($datestring, $dateformat) {
global $comment;
return mysql2date('F j y', $comment->comment_date);
}
add_filter('get_comment_date','change_comment_date',10,2);
// show the comments
if ( have_comments() ) : ?>
<h3 class="comment_title"><?php comments_number('No Comments', 'One Comment', '% Comments' );?></h3>
<ol class="commentlist" id="singlecomments">
<?php wp_list_comments(array('avatar_size'=>48, 'reply_text'=>'Reply', 'type'=>'comment')); ?>
</ol>
<h3 class="trackback_title">Trackbacks / Pingbacks</h3>
<a href="#pings" class="show_trackbacks">show trackbacks</a>
<ol id="pings" class="trackback">
<?php wp_list_comments('type=pings'); ?>
</ol>
<div class="comments-navigation">
<div class="alignleft"><?php previous_comments_link() ?></div>
<div class="alignright"><?php next_comments_link() ?></div>
</div>
<?php else : // this is displayed if there are no comments so far ?>
<?php if ('open' == $post->comment_status) :
// If comments are open, but there are no comments.
else :
// comments are closed
endif;
endif;
if ('open' == $post-> comment_status) :
// show the form
?>
<div id="respond"><h3><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h3>
<div id="cancel-comment-reply">
<small><?php cancel_comment_reply_link(); ?></small>
</div>
<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p>You must be <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php echo urlencode(get_permalink()); ?>">logged in</a> to post a comment.</p>
<?php else : ?>
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform" class="span-17">
<div>
<?php comment_id_fields(); ?>
<input type="hidden" name="redirect_to" value="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>" /></div>
<p><small><strong>XHTML:</strong> You can use these tags: <?php echo allowed_tags(); ?></small></p>
<p><textarea name="comment" id="comment" cols="10" rows="10" tabindex="4"></textarea></p>
<?php if (get_option("comment_moderation") == "1") { ?>
<p><small><strong>Please note:</strong> Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.</small></p>
<?php } ?>
<?php if ( $user_ID ) : ?>
<p class="loggedin">Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>.
<a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Log out of this account">Logout »</a></p>
<?php else : ?>
<p><label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label>
<input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="1" /></p>
<p><label for="email"><small>Email <?php if ($req) echo "(required)"; ?></small></label>
<input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="22" tabindex="2" /></p>
<p><label for="url"><small>Website</small></label>
<input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="22" tabindex="3" /></p>
<?php endif; ?>
<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" /></p>
<?php do_action('comment_form', $post->ID); ?>
</div>
</form>
<?php
endif;
endif;
19 Comments
Leave a CommentHey Curtis, Thanks for the code, that will make it so much easier! Keep up the good work and Thanks again for what you do for those of us that don’t have a clue how to code…. Richard
12th Jan 2009
Thanks ypou for tht man!
5th Feb 2009
Nice Work but it breaks the paginated comments. Any idea how to fix this?
15th Feb 2009
It shouldn’t break comments pagination, I’m using the same code in a theme and it is working. Maybe try using paginate_comments_links in place of the navigation that is there now.
15th Feb 2009
Well even with that paginate funtion it still seems to mess up the paginated comments.. Another thing, I just copied and pasted all your code into my comments file and the javascript hide thing does not work.(I am using the default wordpress theme)
Also It would be good to show the comment count for only the comments not the trackbacks, and for the trackbacks to just be the link not the whole comment.
28th Feb 2009
For the navigation try checking out my WordPress Navigation Helper post. For the javascript to work you’ll need to also have the jQuery framework called.
I’m pretty sure there is a way to only count the comments but I’m not sure of it offhand. To style the trackbacks you’ll need to style those with a callback, there are a few tutorials on the internet.
28th Feb 2009
To only count the comments you nned to drop the following code into your themes functions.php file.
add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
global $id;
$comments_by_type = &separate_comments(get_comments('post_id=' . $id));
return count($comments_by_type['comment']);
}
I’ve got everything working part from the getting the jQuery hide function to work. :?
11th Mar 2009
See my comment below for the jQuery function. If that isn’t the problem check if the link has the show_trackbacks class and the track backs list has the trackback class.
And thanks for posting that code, I’m sure a lot of people will find it helpful.
11th Mar 2009
No problem Curtis, happy to help.
Yeah, I’m using your code but unless I’m misunderstanding something, it isn’t functioning as expected. Clicking on ’show trackbacks’ gives us a nice slide down effect to reveal the trackbacks but clicking it again does not make it slide back up. Hrm. The code is interted in my theme thusly:
$(document).ready(function(){
$('ol.trackback').slideUp();
$('.show_trackbacks').click(function(){
$('ol.trackback').slideDown();
});
});
12th Mar 2009
Ah OK, to get the trackbacks to slide back up change slideDown to slideToggle.
12th Mar 2009
I’m not experienced with jQuery. Where does the slideUp() code go? I don’t see it in the finished code.
8th Mar 2009
Add this right before </body> in your footer.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
// Your code here
});
</script>
8th Mar 2009
Thanks bunches!
10th Mar 2009
I need this, thank you.
16th Dec 2009
You’ve saved me a hell of a lot of work here! Thanks :D
21st Dec 2009
No problem Tom, the code is a wee bit old so if you find any problems with it let me know.
21st Dec 2009
Trackbacks / Pingbacks
show/hide trackbacks[...] complete code, and even some jQuery for a toggle hide switch on the trackbacks, be sure to read the article by Curtis Henson. These icons link to social bookmarking sites where readers can share and discover new web [...]
[...] See the rest here: Separating and Hiding Trackbacks with Jquery in WordPress 2.7 [...]
[...] we wanted to hide trackbacks. There is a tutorial to do this using jquery, but that seemed too technical for me. So I simply deleted out the trackbacks code which we had [...]