Support » Themes and Templates » Comments: bottom of post in index.php like single.php

  • Resolved ljkerley


    In my single PHP template, both the comments and the comments entry form appear at the bottom of the post (without clicking anything). For this I use:
    <?php comments_number(‘0’, ‘1’, ‘%’); ?>
    <?php comments_template(); // Get wp-comments.php template ?>

    In my index and archive templates, I also would like it to work the same way. I can’t get it to work, though. I read in the docs that <?php comments_template(); ?> is “for use in single post and page displays.” OK, so then I need a template tag for index and archive pages.

    Unfortunately the only template tag I have found is <?php comments_popup_link(‘0’, ‘1’, ‘%’); ?>. But this template tag has a very different behaviour. It doesn’t immediately show the comments and comment form. Rather, it gives a hyperlink. And the hyperlink calls the single.php template. The result is that the user must wait for the whole page to load, and then the cursor is positioned at the top of the page. So the user may not notice that the comments and comment form appeared at the bottom of the page. I don’t want to do it this way.

    Is there an alternative out there that behaves like <?php comments_template(); ?> but works in index.php and archive.php?

Viewing 3 replies - 1 through 3 (of 3 total)
  • <?php comments_template(); ?>
    should work in index.php if placed directly after the_content() call

    if you look into the first lines of the code of comments_template()
    (function starts in /wp-includes/comment-template.php line 830 ++) you will find an if statement that restricts the code to single pages and posts, unless the global variable $withcomments is set to true or 1;

    if ( !(is_single() || is_page() || $withcomments) || empty($post) )

    in the front page code (or archives), you need therefore to set this variable before calling comments_template() :

    <?php global $withcomments; $withcomments = 1; comments_template(); ?>

    Hey Alchymyth,
    That worked quickly and easily. Thanks so much!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Comments: bottom of post in index.php like single.php’ is closed to new replies.