我使用了一个助手函数。
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
}
现在,如果没有pingback和trackback
$num
= 0,则不会打印列表的标记。
您可以使用带有post ID的函数来计算当前页面以外的其他post的ping数,如下所示:t5_count_pings( 12345 )
.