Support » Fixing WordPress » Add Comment Count to Post Title

  • I want to add the comment count to the end of my post title. For example,

    “13 Idea-Starters for Stuck Bloggers (23 comments)”

    I currently have this code in the single.php file:

    <h1><?php
    	$title = the_title();
    	$commentcount = comments_number('0', '1', '%');
    	$fulltitle = $title . ' (' . $commentcount . ' comments)';
    	echo $fulltitle;
    	?></h1>

    The post title displays momentarily but then is overwritten by the comment count. What am I doing wrong?

    Thanks for your help.

Viewing 13 replies - 1 through 13 (of 13 total)
  • the_title() usually echos the title and doesn’t store it as a variable. Use get_the_title();
    $title = get_the_title();

    That didn’t quite work either. It is still over-writing. You can see it here: http://tr.im/t08b

    This should work:

    <h1><?php the_title(); ?> <?php comments_number('(No Comments)', '(One Comment)', '(% Comments)' );?></h1>

    Just change the text in the comments_number call to make it say what you want based on the number of comments. I made this change in the single.php file within the theme’s directory.

    Hope it helps,
    Mark H.

    This should work. I got it to work correctly on my site.

    <h1><?php
    global $wpdb;
    $postID = get_the_ID();
    $title = get_the_title();
    $comments = $wpdb->get_row("SELECT comment_count as count FROM wp_posts WHERE ID = '$postID'");
    $commentcount = $comments->count;
    if($commentcount == 1): $commenttext = 'comment'; endif;
    if($commentcount > 1 || $commentcount == 0): $commenttext = 'comments'; endif;
    $fulltitle = $title.' ('.$commentcount.' '.$commenttext.')';
    echo $fulltitle;
    ?></h1>

    Misternifty’s solution totally worked. Thanks so much!

    OK, This will cut down on some loading time and db calls. Intense Debate filters out duplicate comments (i.e. if a user double clicks submit). The wp_comments count and the Intense Debate count sometimes are different. It’s because of the way each platform handles comments it seems.

    <h1><?php
    $commentscount = get_comments_number();
    if($commentscount == 1): $commenttext = 'comment'; endif;
    if($commentscount > 1 || $commentscount == 0): $commenttext = 'comments'; endif;
    echo get_the_title().' ('.$commentscount.' '.$commenttext.')';
    ?></h1>

    Hi,

    Try to add this plugin:

    http://wordpress.org/extend/plugins/liz-comment-counter-by-ozh/

    Thanks,

    Shane G.

    Shane,

    Thanks. But I am not looking for a badge of the total comments for all posts. I just want the comments for the individual post. I think that what misternifty has given me does the trick.

    Thanks,

    Mike

    misternifty,

    comments_popup_link seems to get the correct number of comments when using IntenseDebate. However, there doesn’t appear to be the equivalent get_comments_popup_link. As a result, if I try to use it, it produces “Comments (23)”. I can’t seem to get JUST the number, like I can with get_comments_number.

    Do you or does anyone else have a workaround?

    If possible, I’d really lik the number to reflect the actual number of comments in Intense Debate for the particular post.

    Thanks.

    Hey Michael,

    I can’t see anything that would work other than re-importing your comments into Intense Debate. I’ve been reading up and it seems like ID has some issues here. Maybe we can post it on their forums and get an answer. The issue seems to reside in their pond.

    Okay, cool. Thanks.

    Here’s a pretty cool tutorial on how to do this.

    mcateee

    (@mcateee)

    I am using IntenseDebate and ran into some problems with the comments_number() call continuing to display extra text. So instead, I used the following:

    <?php $commentscount = get_comments_number(); echo $commentscount; ?>

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Add Comment Count to Post Title’ is closed to new replies.