0

If there are comments, I want to wrap the amount eg "5 comments" in the link to the comments section of the post, but when I add the echo for comments_link it errors.

<?php comments_number( 
'', 
'<span class="comment_meta">With:</span> 1 Comment', 
'<span class="comment_meta">With:</span> <a href="'echo . $comments_link(); . '">% Comments</a>' ); 
?>

Think I'm having trouble dipping in and out of php and html. Any help would be very much appreciated.

==== EDIT ====

Not sure if get_comment_count is still used but I've got it working using the below:

<?php $comment_count = get_comment_count($post->ID); ?>
<?php if ($comment_count['approved'] > 0) : ?><span class="comment_meta">With: </span><?php comments_popup_link( 
'', // No comments exist, you would probably want to display a link here in order for people to add the first comment
'1 comment', // 1 comment, usually phrased differently
'% comments' // > 1 comment
); ?>
<?php endif; ?>
1

The problem is that you are echoing inside a string concatenation. Think of echo as something that will always begin on a new line. In most cases it's wisest to perform any calculations or string concatenations and store the result in a variable, and then call echo $var; on the following line.

In this particular case though we are dealing with a WordPress core function (comments_popup_link()) that does the echoing for us. This will probably work:

<span class="comment_meta">With: </span><?php comments_popup_link(
    '', // No comments exist, you would probably want to display a link here in order for people to add the first comment
    '1 comment', // 1 comment, usually phrased differently
    '% comments' // > 1 comment
); ?>

If you don't want to display the "With:" label in case there are no comments you could just put it inside an if statement. I think thismight work:

<span class="comment_meta"><?php if (wp_count_comments(get_the_ID())) : ?>With: <?php endif; ?></span>
  • Thanks, this works nicely, except when I add the if check round the label, it breaks: <?php if (wp_count_comments(get_the_ID()) ?><span class="comment_meta">With: </span><?php endif ?> – Xav Feb 20 '13 at 21:17
  • Whoops, that line was missing a colon. I updated my answer with a fix. – Simon Feb 20 '13 at 21:22
  • Still doesn't like it... – Xav Feb 20 '13 at 21:27
  • Was also missing a parenthesis, updated again. Try reading the PHP notices/warnings when that happens. The information needed to solve such problems are in there 99% of the time. – Simon Feb 20 '13 at 21:42
  • I need to turn debug on, just getting white screens at the moment. It no longer errors but it does still output the label...somehow – Xav Feb 20 '13 at 21:51
0

You should not echo your comments_link.

  <a href="<?php comments_link(); ?>"><?php comments_number('Be the first to comment!', '1 comment.', '% comments already!'); ?></a>

or use something like this

<?php echo sprintf(__('<a href="%s">%s comment(s)</a>', 'textdomain'), get_comments_link(),get_comments_number()); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.