0

Can someone please shed some light into echoing html when it contains php functions? I am trying to echo out the following but cannot find a useful reference.

<a href="<?php comments_link(); ?>"><?php comments_number('<span class="nocomments">comments</span>', '<span class="onecomment">1 comment</span>', '<span class="morecomments">% comments</span>'); ?></a>

Grateful for any help!

Updated the code below, I hope I am making myself clear. If the comments don't open/have been closed I am trying to echo out the else condition as text.

<?php
if ( comments_open() ) :
echo '<p>';
comments_popup_link( '<span class="one-comment">leave a comment</span>', '<span class="one-comment">1 comment</span>', '<span class="more-comments">% comments</span>', '');
echo '</p>';
else 
echo 

the following:

<a href="<?php comments_link(); ?>"><?php comments_number('<span   class="zerocomments">comments</span>', '<span class="onecomment">1 comment</span>',    '<span class="morecomments">% comments</span>'); ?></a>
4
  • 1
    Are you trying to "echo" that as a string of text in your program? Commented Oct 4, 2012 at 22:26
  • there is no echoing in the code provided... please provide all relevant code to your question, as we cannot know what your 2 functions do (or should do) Commented Oct 4, 2012 at 22:29
  • Okay, what exactly are you hoping to display using this function? What do you know about the output you are expecting? This function is part of wordpress, not standard PHP so there is not a lot of info out there on its output. Commented Oct 4, 2012 at 22:57
  • Wordpress core functionality can't distinguish between the two different cases when comments have been closed after they were open for some time and when the comments for a post were off from the start. The existing comment count functions can't display all the various possibilities, so I figured they need to be combined to get it right. If the comments are open the comments_popup_link function will display the number of comments. If comments are closed the comments_number function will account for showing the correct count, which isn't possible with the comments_popup_link alone. Commented Oct 4, 2012 at 23:20

2 Answers 2

2
<?php
if (comments_open()) {
    echo '<p>';
    comments_popup_link( '<span class="one-comment">leave a comment</span>', '<span    class="one-    comment">1 comment</span>', '<span class="more-comments">% comments</span>', '');
    echo '</p>';
} else {
?>
    <a href="<?php echo comments_link(); ?>"><?php echo comments_number('<span   class="zerocomments">kommentarer</span>', '<span class="onecomment">1 kommentar</span>',    '<span class="morecomments">% kommentarer</span>'); ?></a>
<?php
}
?>

The best way to echo HTML when it contains PHP functions is to break back out of PHP, as done above.

I'm not sure exactly what you're trying to do, but the rest is up to you. :)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! Looks like I can use this with my very basic skills. I'll give it a shot and see if I can get it to work.
I modified my answer, now that I think I understand what you're wanting better.
Thank you so much Jonathan! I'll try your suggestion and update shortly. I explained what I am trying to accomplish in a comment above.
Your solution works right out of the box! Thank you, I am most grateful for your help! It was the final hurdle in what has been weeks of work to get the comment function to work correctly.
0

try it like so:

<a href="<?php echo comments_link(); ?>"><?php echo comments_number('<span class="nocomments">comments</span>',     '<span class="onecomment">1 comment</span>', '<span class="morecomments">% comments</span>'); ?></a>

It looks like you just need to explicitly call the echo function.

2 Comments

I should've mentioned it needs to be echoed inside an if...else statement
Then you will need to show the code as it exists within that if else. This can still be done easily though, so never fear....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.