WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Comments: bottom of post in index.php like single.php (4 posts)

  1. ljkerley
    Member
    Posted 3 years ago #

    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?

  2. Samuel B
    moderator
    Posted 3 years ago #

    <?php comments_template(); ?>
    should work in index.php if placed directly after the_content() call

  3. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    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) )
     return;

    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(); ?>

  4. ljkerley
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic