Quantcast
Viewing all articles
Browse latest Browse all 15

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

The post Comment Link in single.php appeared first on Nathan Rice.


Viewing all articles
Browse latest Browse all 15

Trending Articles