获取帖子评论和ping总数的最快方法

时间:2010-11-28 作者:Alex

我正在制作自己的评论模板(like this) 我需要知道如何获得当前帖子的评论和ping计数,也许可以使用快速数据库查询或类似的方法?

请注意,我无法使用count($comments) 或者诸如此类的,因为我没有运行默认值comments_template() 从数据库中获取所有注释的函数。相反,我只使用get_comments().

$post->comment_count (显然由初始化get_post) 很接近我要查找的内容,但它同时包含评论和ping:(

1 个回复
最合适的回答,由SO网友:bueltge 整理而成

您可以在函数中使用此自定义函数。主题php:

/**
 * count for trackback, pingback, comment, pings
 *
 * embed like this:
 * fb_comment_type_count(\'pings\');
 * fb_comment_type_count(\'comment\');
 */
if ( !function_exists(\'fb_comment_type_count\') ) {
        function fb_get_comment_type_count( $type=\'all\', $zero = false, $one = false, $more = false, $post_id = 0) {
                global $cjd_comment_count_cache, $id, $post;

                if ( !$post_id )
                        $post_id = $post->ID;
                if ( !$post_id )
                        return;

                if ( !isset($cjd_comment_count_cache[$post_id]) ) {
                        $p = get_post($post_id);
                        $p = array($p);
                        fb_update_comment_type_cache($p);
                }
                ;
                if ( $type == \'pingback\' || $type == \'trackback\' || $type == \'comment\' )
                        $count = $cjd_comment_count_cache[$post_id][$type];
                elseif ( $type == \'pings\' )
                        $count = $cjd_comment_count_cache[$post_id][\'pingback\'] + $cjd_comment_count_cache[$post_id][\'trackback\'];
                else
                        $count = array_sum((array) $cjd_comment_count_cache[$post_id]);

                return apply_filters(\'fb_get_comment_type_count\', $count);
        }

        // comment, trackback, pingback, pings, all
        function fb_comment_type_count( $type=\'all\', $zero = false, $one = false, $more = false, $post_id = 0 ) {

                $number = fb_get_comment_type_count( $type, $zero, $one, $more, $post_id );
                if ($type == \'all\') {
                        $type_string_single = __(\'Kommentar\', FB_BASIS_TEXTDOMAIN);
                        $type_string_plural = __(\'Kommentare\', FB_BASIS_TEXTDOMAIN);
                } elseif ($type == \'pings\') {
                        $type_string_single = __(\'Ping und Trackback\', FB_BASIS_TEXTDOMAIN);
                        $type_string_plural = __(\'Pings und Trackbacks\', FB_BASIS_TEXTDOMAIN);
                } elseif ($type == \'pingback\') {
                        $type_string_single = __(\'Pingback\', FB_BASIS_TEXTDOMAIN);
                        $type_string_plural = __(\'Pingbacks\', FB_BASIS_TEXTDOMAIN);
                } elseif ($type == \'trackback\') {
                        $type_string_single = __(\'Trackback\', FB_BASIS_TEXTDOMAIN);
                        $type_string_plural = __(\'Trackbacks\', FB_BASIS_TEXTDOMAIN);
                } elseif ($type == \'comment\') {
                        $type_string_single = __(\'Kommentar\', FB_BASIS_TEXTDOMAIN);
                        $type_string_plural = __(\'Kommentare\', FB_BASIS_TEXTDOMAIN);
                }

                if ( $number > 1 )
                        $output = str_replace(\'%\', number_format_i18n($number), ( false === $more ) ? __(\'%\', FB_BASIS_TEXTDOMAIN) . \' \' . $type_string_plural : $more);
                elseif ( $number == 0 )
                        $output = ( false === $zero ) ? __(\'Keine\', FB_BASIS_TEXTDOMAIN) . \' \' . $type_string_plural : $zero;
                else // must be one
                        $output = ( false === $one ) ? __(\'Ein\', FB_BASIS_TEXTDOMAIN) . \' \' . $type_string_single : $one;

                echo apply_filters(\'fb_comment_type_count\', $output, $number);
        }
}
此函数提供pingback、trackback、comments或all的计数,例如:

<h2 class="comments"><?php fb_comment_type_count( \'comment\' ); ?></h2>
可以使用以下参数返回计数器:comment、trackback、pingback、pings或all

结束

相关推荐

在wp-fig.php中更改‘WPLANG’只会影响管理语言,还是会产生其他后果?

我很想知道是否在wp config中设置“WPLANG”。php只会影响管理语言,还是会产生其他后果?我用外语写博客,但在我的管理中使用英语。不久前,我最初将WPLANG设置为外语,并使用插件(英语中的admin)在使用时保留英语管理界面。翻译主题的mo文件。我现在使用“WPML”来管理翻译。我想知道我的WPLANG设置是否仍然相关,以及它对站点的真正意义是什么?谢谢