Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I managed to get my comments numbered and then to separate the comments from the pingbacks and trackbacks. The only problem I have now is that I don't want to show the title of this section (where the trackbacks/pingbacks are) unless there are any.

This is the code I'm using in comments.php:

<div class="pingback"><h3>Trackbacks/Pingbacks:</h3>
        <?php   
        wp_list_comments(array(
          'callback'=>'list_pings',
          'style'=>'ol',
          'type'=>'pings',
        ));
        ?> 
</div>

And this is the code from functions.php:

<?php
function list_pings($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
?>
<li id="comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?>
<?php } ?>
<?php
add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
if ( ! is_admin() ) {
global $id;
$comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
return count($comments_by_type['comment']);
} else {
return $count;
}
}
?>

I think I should do an if/else statement but I can't figure out where to start.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

I am using a helper function for that.

functions.php

/**
 * Count amount of pingbacks + trackbacks for a post.
 *
 * @param int $post_id Post ID for comment query. Default is current post.
 * @return int
 */
function t5_count_pings( $post_id = NULL )
{
    $pings    = 0;
    $comments = FALSE;

    if ( NULL !== $post_id )
    {
        $comments = get_comments(
            array (
                'post_id' => $post_id, # Note: post_ID will not work!
                'status'  => 'approve'
            )
        );
    }
    elseif ( ! empty ( $GLOBALS['wp_query']->comments ) )
    {
        $comments = $GLOBALS['wp_query']->comments;
    }

    if ( ! $comments )
        return 0;

    foreach ( $comments as $c )
        if ( in_array ( $c->comment_type, array ( 'pingback', 'trackback' ) ) )
            $pings += 1;

    return $pings;
}

comments.php

if ( $num = t5_count_pings() )
{
?>
<h2 id="pingbacks"><?php
    printf( _n( 'One pingback', '%d pingbacks', $num, 't5_theme' ), $num );
?></h2>
<ol class="pinglist ■ sans-font">
<?php
wp_list_comments(
    array (
        'type'     => 'pings',
        'style'    => 'ul',
        'callback' => 't5_list_pings_callback'
    )
);
?></ol>
<?php
}

Now, if there are no pingbacks and no trackbacks $num = 0, and the markup for the list will not be printed.

You can use the function with a post ID to count the number of pings for another post than the current page like this: t5_count_pings( 12345 ).

share|improve this answer
    
Thanks, this worked great! –  George Grigorita Feb 2 '13 at 20:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.