1

On my single.php and index.php I'm including the comment entry routine with this code...

<?php if(get_option('allow_comments_posts')){comments_template();} ?>

However, when the specific post being viewed in single.php has "Allow Comments" unchecked, I don't want the comment template to appear.

I was under the impression that the comments_template() routine automatically managed this, but apparently I need to wrap it or pass a paramater?

1 Answer 1

2

As far as I remember main purpose of comments_template() is to load template and specific logic should be handled inside that template.

Snippet from Twenty Ten comments.php:

if ( ! comments_open() ) :
?>
    <p class="nocomments"><?php _e( 'Comments are closed.', 'twentyten' ); ?></p>
<?php endif; // end ! comments_open() ?>
4
  • thanks for pointing me there. Apparently, something is amiss with my $post variable or scope. Even though the current post I'm looking at has "allow comments" unchecked, comments_open() is returning true. Any ideas?
    – Scott B
    Commented Jan 21, 2011 at 19:55
  • Ah, I think I see what's happening. I have a "Related Articles" listing just before the comments_template() call. The $post->ID is getting reset to the last post in the link list for those articles. Should I redeclare $post inside comments.php? If so, $post = ?
    – Scott B
    Commented Jan 21, 2011 at 20:04
  • @Scott B try wp_reset_query() after your related section
    – Rarst
    Commented Jan 21, 2011 at 20:06
  • BINGO! That did it. Thanks Rarst. As always, you're right on the money :)
    – Scott B
    Commented Jan 21, 2011 at 20:12

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.