由于WordPress 4.0,您可以使用该函数get_comments_number_text()
. 参见票证#10177.
旧答案
创建包装函数:过滤comments_number()
, 将其存储在包装器中,并向本机函数返回一个空字符串。return
包装器函数中的实际字符串。
/**
* Wrapper for native comments_number().
*
* This has two functions:
* 1. It is can be called from a theme or plugin to get the comments number
* string *returned* for a variable.
* 2. It sets itself as a temorary filter to catch the string. The filter will
* be removed immediately afterwards.
*
* @wp-hook comments_number
* @param string $zero Template for no comments OR the parsed string
* when used as filter.
* @param string $one
* @param string $more
* @return string
*/
function t5_get_comments_number( $zero = FALSE, $one = FALSE, $more = FALSE )
{
static $output = \'\';
if ( \'comments_number\' === current_filter() )
{
remove_filter( current_filter(), __FUNCTION__ );
$output = $zero;
return \'\';
}
else
{
add_filter( \'comments_number\', __FUNCTION__ );
comments_number();
return $output;
}
}
用法:
$comm_num = t5_get_comments_number();
print "We found $comm_num.";
// Prints for example: We found 51 Comments.