Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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
 */
?>
share|improve this answer
    
So basically, it's not possible to use wp_list_comments() outside of comments.php? –  Richard B Feb 16 '12 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()). –  Chip Bennett Feb 16 '12 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 '12 at 15:17
    
I leave that as an exercise for the user, since it will involve re-implementing the functionality within the comments_template() function. –  Chip Bennett Feb 16 '12 at 15:19
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

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.