Forums

Add Comment Count to Post Title (14 posts)

  1. michaelhyatt
    Member
    Posted 2 years ago #

    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.

  2. misternifty
    Member
    Posted 2 years ago #

    the_title() usually echos the title and doesn't store it as a variable. Use get_the_title();
    $title = get_the_title();

  3. michaelhyatt
    Member
    Posted 2 years ago #

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

  4. MarkRH
    Member
    Posted 2 years ago #

    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.

  5. misternifty
    Member
    Posted 2 years ago #

    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>
  6. michaelhyatt
    Member
    Posted 2 years ago #

    Misternifty's solution totally worked. Thanks so much!

  7. misternifty
    Member
    Posted 2 years ago #

    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>
  8. Shane G
    Member
    Posted 2 years ago #

    Hi,

    Try to add this plugin:

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

    Thanks,

    Shane G.

  9. michaelhyatt
    Member
    Posted 2 years ago #

    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

  10. michaelhyatt
    Member
    Posted 2 years ago #

    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.

  11. misternifty
    Member
    Posted 2 years ago #

    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.

  12. michaelhyatt
    Member
    Posted 2 years ago #

    Okay, cool. Thanks.

  13. michaelhyatt
    Member
    Posted 2 years ago #

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

  14. mcateee
    Member
    Posted 2 years ago #

    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; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic