Comment Link in single.php

Ever noticed that the comments_popup_link function doesn’t actually output anything when viewing a single post?

I have … and until recently, I ended up just not including a link to the “comments” of a post when viewing a single post, and just hoping that either 1) no one noticed, or 2) no one would care, assuming they would just scroll down to the bottom of the post to see the number of comments.

But, I don’t like to assume. I like to think of the user as a person … a person that has normal expectations when using a theme.

Therefore, I decided to figure out a way (I’m sure I’m not the only one) to keep that darn link working … even when viewing a single post.

So, let’s assume that you have some variation of the following code in the index.php/home.php file of your WordPress theme:

<?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?>

This is the quick and dirty way of handling the link to the comments section of a single post. But, like I said, when viewing a single post, this doesn’t output anything. So, replace that code up there with the following:

<a href="<?php the_permalink(); ?>#comments"><?php comments_number('No Comments', '1 Comment', '% Comments'); ?></a>

I found this after noticing that the particular comments.php file I use displayed the number of comments in the header of the comments section by using the comments_number function. Bingo! That’s all I needed! Once you’ve found a function that will output the number of comments, it’s just a matter of formulating a link to the posts permalink followed by the section anchor (#section).

And now, you’ve fabricated the same output as comments_popup_link manually (with a little dynamic help).

Hope that helps!!!

30 Replies to “Comment Link in single.php”

  1. I noticed that a long time ago, but never thought about displaying it using comments_number. I simply don’t use comments_popup_link for postmeta in single.php because two comments numbers (one in postmeta and one in the comments template)on the same page means one of them is usless.

    There is an exception and that’s when you’re displaying post meta data directly below the post title.

  2. @Small Potato

    Yeah, and I happen to be in that exact situation right now, given the nature of the postmeta section I’m doing for Stripped. I found it necessary, so I had to find a way to make it happen.

    BTW, did you make the plunge into using a feed reader?

  3. I have a Google Reader account, but I don’t use it 🙂

  4. I puzzled over this not 3 hours ago — nice work. Any way to to remove the anchor tag if there are no comments?

  5. @Darren Hoyt

    Sure. Just put the comment_number into a variable (but make sure to have it output 0,1,% instead of the words), then do a php if statement. If the comment_number is equal to 0, then have it just print the comment_number (this time with the words) without the anchor. Then a simple else can handle the anchor. Easy enough, but I’m a little too lazy to write out the code myself right now. Maybe I’ll update the post later with the code.

  6. That’s fab thanks, was just puzzling over how to work this out.

  7. Used this tip to resolve some issues at http://www.bravesblast.com. Thanks for the help. Never had looked at doing it that way before.

  8. Hello!
    I think this try.

  9. I put the code in, it displays fine on mizpah.tv, on individual posts, however when you click on the link nothing seems to happen!

  10. I see, the link works if there are comments, but not if there are none as #respond must be used?

  11. Hi,
    I think it is a very useful information.
    I will try this later.
    Thank you

  12. I tried this and it doesn’t work. I just get a link of the above php code. Any thoughts?

  13. very nice. works perfectly.

    thank you very much. great solution !

  14. I noticed the same flaw but had no idea how to fix it. Thanks for your help!

  15. This is what I looking for 😉

  16. Dude, you saved my bacon on this one!

    Thanks so much Nathan.

  17. Thank you for the code. This helped me out on my new blog. I was wondering why that pesky comment link was broken.

  18. it really helps 😛
    i didn’t know the fn before coming on http://wordpress.org/support/topic/231678
    so I replaced the

    just with the
    … ^^

  19. oo,the function is banned…

  20. corrected code that works at 100%
    Enjoy 🙂

    <?php comments_popup_link(‘No Comments »‘, ‘1 Comment »‘, ‘% Comments »‘); ?>

  21. How can I write code in comments here?

    1. Always wrap code in opening and closing “pre” tags.

  22. <?php comments_number(‘No Comments »‘, ‘1 Comment »‘, ‘% Comments »‘); ?>

  23. I tried with the tag still not working

    1. try putting it between “code” tags.

  24. <?php comments_number(‘<a href="#respond">No Comments »</a>’, ‘<a href="#comments">1 Comment »</a>’, ‘<a href="#comments">% Comments »</a>’); ?>

  25. ok 🙂
    corrected code that works at 100%
    Enjoy 🙂

    (And with this site, I could write code here http://people.w3.org/rishida/scripts/uniview/conversion.php)

    <?php comments_number(‘<a href="#respond">No Comments »</a>’, ‘<a href="#comments">1 Comment »</a>’, ‘<a href="#comments">% Comments »</a>’); ?>

  26. Thanks you very much. But I don’t know why in single.php the comments_popup_link don’t work default.

  27. since i updated to 2.8 i can’t insert thumbnails on my site?, also i want to change the word category to Gallery on my front page. HELP…

Comments are closed.