The following code:

echo '<ul>';
while ( $query->have_posts() ) {

    $title = get_the_title();
    echo '<li>' . $title . comments_number() . '</li>';
echo '</ul>';

moves the comments ABOVE and outside of the <li> like this:

1 Comment 

Hello world!

1 Answer 1


You want to use get_comments_number() as that doesn't echo the results, while comments_number does echo

Your Answer

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

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