WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I cannot get the comments_popup_link to display with the posts retrieved by the foreach loop. Only the title and permalink are displayed. Any idea why the comment link is left out?

    echo '<div id="tab-news" class="tab-all"><a id="link-news" class="link-all" href="/news">News</a></div><ul>';
    foreach($ids as $id){
       echo '<li><a href="'.get_permalink( $id ).'">'.get_the_title( $id ).'</a>';
       if ( comments_open() ) :
       echo '<p>';
       comments_popup_link( '', '<span class="onecomment">1 comment</span>', '<span class="morecomments">% comments</span>', '');
       echo '</p>';
    echo '</li></ul>';
share|improve this question
well, it might be stupidly trivial for me to ask, but do you have comments open on the global site configuration ? or maybe disabled on specific posts ? – krembo99 Feb 27 '12 at 3:29
up vote 0 down vote accepted

the comments_popup_link() function will only work when used within the_loop; a 'foreach' loop using post Ids is not the same.

you could try to build your own link, based on this function: get_comments_number( $id ).

share|improve this answer
Thank you! Realized it as well and gave up on the idea and used the normal wordpress loop. The advantage of the foreach loop is that it lets me separate logic and presentation. – sarytash Feb 27 '12 at 18:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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