WordPress.org

Support

Support » Themes and Templates » [Resolved] Creating custom comment link text

[Resolved] Creating custom comment link text

  • 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:

    <p>
    <?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.
    </p>

    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • If you’re willing enclose the whole thing in the link, you could just change:
    <?php comments_number('no comments', 'one comment', '% comments');?>
    to:
    <?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.

    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!

    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Creating custom comment link text’ is closed to new replies.