Separate Pings/Trackbacks from Total Comment Count in WordPress

After tak­ing a look at some search results and at Matt Martz’ arti­cle, Sep­a­rat­ing Pings from Com­ments in Word­Press 2.7, I still had some issues with par­tic­u­lar­ly the com­ment count. There were also a prob­lem with how the func­tions I have added/modified are being car­ried across in my tem­plates (but prob­a­bly are most­ly in the scope of this Blue­print theme). The files that we’re most­ly going to deal with are these ones: comments.php, functions.php and single.php.

Any­ways, I just want to note here how I took care of them and what I end­ed up with.

First and fore­most, in my functions.php file, I added the fol­low­ing to only count com­ments 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 == ""){
    return $comment_count;
} ?>

Also in functions.php, the fol­low­ing func­tion is to sim­pli­fy the list­ing of pings/trackbacks lat­er 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 snip­pet of code near my post’s head­er. I am going to dis­play “Com­ment” if there are no com­ments and have it linked to #respond for them to leave one if they would like to. If there are com­ments, I dis­play the respec­tive linked text.

<?php if ( comments_open() ) {
    echo ' &nbsp;|&nbsp; ';
    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 snip­pet as we can’t use the func­tion comments_popup_link() in this page.

Mean­while, in the comments.php file, I have the fol­low­ing block to dis­play com­ments & pings/trackbacks sep­a­rate­ly:

<?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 } ?>

        <ol class="commentlist">
            <?php wp_list_comments('type=comment&avatar_size=48') ?>
        <div class="navigation">
            <div class="alignleft"><?php previous_comments_link() ?></div>
            <div class="alignright"><?php next_comments_link() ?></div>
    <?php endif; ?>
    <?php if ( ! empty($comments_by_type['pings']) ) : ?>
        <div id="pings">
            <ol class="commentlist">
                <?php wp_list_comments('type=pings&callback=list_pings'); ?>
    <?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; ?>


Comments Feed


  1. Separate Pings/Trackbacks from Total Comment Count in WordPress … | thetrackbacksecrets