1

I'm simply typing wp_list_comments() in page.php but nothing is shown. But if I put the comments_template() tag there, then the comment form, comments and pings are shown. Why is that, why can't I use wp_list_comments() in page.php? What could I have done wrong?

2 Answers 2

1

You need to include comments functionality via comments_template(), because this function doesn't merely include the comments.php, file, but also handles all of the querying and functions required for displaying comments.

While the Codex doesn't really get into details, you can see, by looking at source, what's going on:

<?php
/**
 * Loads the comment template specified in $file.
 *
 * Will not display the comments template if not on single post or page, or if
 * the post does not have comments.
 *
 * Uses the WordPress database object to query for the comments. The comments
 * are passed through the 'comments_array' filter hook with the list of comments
 * and the post ID respectively.
 *
 * The $file path is passed through a filter hook called, 'comments_template'
 * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
 * first and if it fails it will require the default comment template from the
 * default theme. If either does not exist, then the WordPress process will be
 * halted. It is advised for that reason, that the default theme is not deleted.
 *
 * @since 1.5.0
 * @global array $comment List of comment objects for the current post
 * @uses $wpdb
 * @uses $post
 * @uses $withcomments Will not try to get the comments if the post has none.
 *
 * @param string $file Optional, default '/comments.php'. The file to load
 * @param bool $separate_comments Optional, whether to separate the comments by comment type. Default is false.
 * @return null Returns null if no comments appear
 */
?>
4
  • So basically, it's not possible to use wp_list_comments() outside of comments.php?
    – Richard B
    Feb 16, 2012 at 15:05
  • I won't say that it's not possible. Also: it would be more accurate to say that the comments template is expected to be included via comments_template() (since the filename doesn't have to be comments.php; you can pass a different filename as a parameter to comments_template()). Feb 16, 2012 at 15:07
  • So, just for the record, is it possible to use wp_list_comments inside page.php to list comments? How?
    – Richard B
    Feb 16, 2012 at 15:17
  • 1
    I leave that as an exercise for the user, since it will involve re-implementing the functionality within the comments_template() function. Feb 16, 2012 at 15:19
0

Try enclosing wp_list_comments() like this:

<ol class="commentlist">
<?php wp_list_comments();?>
</ol>

comments_template() simply loads comments.php which makes use of wp_list_comments in an enclosing CSS class.

HTH

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.