Comment Link in single.php

January 9th, 2008 Nathan Rice

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!!!

The Tags: , , , , ,

12 Responses to “Comment Link in single.php”

Small Potato Said:

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.

Comment made on January 10th, 2008 at 10:38 am
Nathan Rice Said:

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

Comment made on January 10th, 2008 at 10:42 am
Small Potato Said:

I have a Google Reader account, but I don’t use it :)

Comment made on January 10th, 2008 at 4:44 pm
Darren Hoyt Said:

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

Comment made on January 20th, 2008 at 7:44 pm
Nathan Rice Said:

@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.

Comment made on January 20th, 2008 at 7:52 pm
Sarah Said:

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

Comment made on January 25th, 2008 at 4:47 am
Jonathan Said:

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.

Comment made on March 6th, 2008 at 7:54 am
Floroskop Said:

Hello!
I think this try.

Comment made on March 19th, 2008 at 12:56 am
Mizpah Said:

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!

Comment made on May 22nd, 2008 at 10:12 am
Mizpah Said:

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

Comment made on May 22nd, 2008 at 10:18 am
buatblogpercuma Said:

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

Comment made on July 21st, 2008 at 11:45 am
ifyvu Said:

Great site! Diamond stealth video card wekne

Comment made on August 19th, 2008 at 12:06 pm

Trackbacks

 

Leave a Comment