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 )
.