[resolved] Creating custom comment link text (4 posts)

  1. Jeff
    Posted 8 years ago #

    I'm working on a theme where I'd like the comment string at the end of a post to appear as a phrase, with only the number of comments linked... the equivalent of:

    There are no comments on this post.

    I also need the text to switch "There are" and "There is" depending on the number of comments, of course.

    So here's what I've been working with:

    <?php if($****){ echo "There is ";} else {echo "There are ";} ?>
    <a href="<?php comments_link(); ?>">
    <?php comments_number('no comments', 'one comment', '% comments');?></a> on this story.

    My problem is, I can't find anything to switch off of -- is there a variable or function I can use to grab the number of comments on a post? Or am I going about this completely wrong way.

  2. ikiru
    Posted 8 years ago #

    If you're willing enclose the whole thing in the link, you could just change:
    <?php comments_number('no comments', 'one comment', '% comments');?>
    <?php comments_number('There are no comments on this post.', 'There is one comment on this post.', 'There are % comments on this post.');?>

    Assuming you thought of that and found it inadequate, how about get_comments_number? It's not documented in the Codex, but the code of the comments_number function (which can be found here) calls it and it seems to be returning an integer.

  3. Jeff
    Posted 8 years ago #

    Awesome! Thanks, ikiru -- I was trying to avoid enclosing the whole thing in a link, but get_comments_number was exactly what I was looking for!

  4. post_break
    Posted 7 years ago #

    Can you please post a guide to getting this to work. I am not so good with php but this is exactly what I am looking for.

    I get the gist of what is happening I just dont know where to put the code.

Topic Closed

This topic has been closed to new replies.

About this Topic