After taking a look at some search results and at Matt Martz’ article, Separating Pings from Comments in WordPress 2.7, I still had some issues with particularly the comment count. There were also a problem with how the functions I have added/modified are being carried across in my templates (but probably are mostly in the scope of this Blueprint theme). The files that we’re mostly going to deal with are these ones: comments.php, functions.php and single.php.
Anyways, I just want to note here how I took care of them and what I ended up with.
First and foremost, in my functions.php file, I added the following to only count comments and not the pings/trackbacks:
<?php add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
global $id;
$comments = get_approved_comments($id);
$comment_count = 0;
foreach($comments as $comment){
if($comment->comment_type == ""){
$comment_count++;
}
}
return $comment_count;
} ?>
Also in functions.php, the following function is to simplify the listing of pings/trackbacks later on (used in comments.php):
<?php function list_pings($comment, $args, $depth) {
// Simply trackbacks/pingbacks
$GLOBALS['comment'] = $comment; ?>
<li id="comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?>
<?php } ?>
In single.php, I have the snippet of code near my post’s header. I am going to display “Comment” if there are no comments and have it linked to #respond for them to leave one if they would like to. If there are comments, I display the respective linked text.
<?php if ( comments_open() ) {
echo ' | ';
if ( get_comments_number() == 0 ) {
echo '<a href="#respond">Comment</a>';
}
if ( get_comments_number() == 1 ) {
echo '<a href="#comments">1 Comment</a>';
}
if ( get_comments_number() > 1 ) {
echo '<a href="#comments">' . get_comments_number() . ' Comments</a>';
}
} ?>
I used the above snippet as we can’t use the function comments_popup_link() in this page.
Meanwhile, in the comments.php file, I have the following block to display comments & pings/trackbacks separately:
<?php if ( have_comments() ) : ?>
<?php if ( ! empty($comments_by_type['comment']) ) : ?>
<h2 id="comments"><?php comments_number('No Comments', 'Comments', 'Comments' );?></h2>
<div class="small">
<span class="feedlink"><?php comments_rss_link('Feed'); ?></span>
<?php if ('open' == $post-> ping_status) { ?><span class="trackbacklink"><a href="<?php trackback_url() ?>" title="Copy this URI to trackback this entry.">Trackback Address</a></span><?php } ?>
</div>
<ol class="commentlist">
<?php wp_list_comments('type=comment&avatar_size=48') ?>
</ol>
<div class="navigation">
<div class="alignleft"><?php previous_comments_link() ?></div>
<div class="alignright"><?php next_comments_link() ?></div>
</div>
<?php endif; ?>
<?php if ( ! empty($comments_by_type['pings']) ) : ?>
<div id="pings">
<h2>Trackbacks/Pingbacks</h2>
<ol class="commentlist">
<?php wp_list_comments('type=pings&callback=list_pings'); ?>
</ol>
</div>
<?php endif; ?>
<?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 ?>
<p class="nocomments">Comments are closed.</p>
<?php endif;
endif; ?>
Thanks for the comment_count() filter.
Was quite helpful, especially in the functions.php!
Glad it helped Ray.