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'm making my own comment template (like this) and I need to know how can I get the comment and ping count for the current post, maybe using a fast database query or something like that?

Note that I can't use count($comments) or anything like that, because I'm not running the default comments_template() function which gets all the comments from the database. Instead I'm pulling only the newest 10 comments using get_comments().

$post->comment_count (apparently initialized by get_post) is close to what I'm looking for, but it counts both comments and pings :(

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

you can use this custom function in the functions.php of the theme:

/**
 * 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);
        }
}

this function give you the count of pingback, trackback, comments or all, example:

<h2 class="comments"><?php fb_comment_type_count( 'comment' ); ?></h2>

you can use the follow parameter for return the counter: comment, trackback, pingback, pings or all

share|improve this answer
1  
that doesn't work. you have calls to some weird functions there, which are not available in wp.. –  Alex Dec 12 '10 at 0:10
    
sorry, i dont fint a call; but maybe you can meore read on the svn with all examples in my free WPBasis Theme: code.google.com/p/wp-basis-theme/source/browse/trunk/basis/… Is also available in html5 –  bueltge Dec 12 '10 at 20:54
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.