How to make comments work for a post loaded per Ajax?

To quote the Codex on the have_comments function:

This function relies upon the global $wp_query object to be set – this is usually the case from within The Loop

The problem is that your ajax handler creates its own WP_Query object. Note that you are not calling the_post(), instead you are calling $posti->the_post(). Same logic applies to comments as well.

Try the following:

if ($posti->have_comments()) {
    echo "Comments ok";
}  else {
    echo "No comments";
}

Leave a Comment