0

I am trying to create a page which displays all the posts in the blog, with the comments to each post listed below the respective post. By looking around the internet I figured out how to create a page template with the following code:

  <?php
$args = array( 'numberposts' => -1);
$posts= get_posts( $args );
if ($posts) {
    foreach ( $posts as $post ) {
        setup_postdata($post);
        Echo "<h1>"; the_title(); Echo "</h1>";
        Echo "<p><small>";the_time('jS F Y'); Echo "</small></p>";
        the_content();
    }
}
?>

This works and an an happy with it. But I am stumped as to how to get the individual post comments to appear as well. Everything I have tried so far doesn't work. Any help would be much appreciated.

Thanks!

  • Link to the source of the code please. – Brad Dalton Aug 1 '14 at 0:25
  • I'll give you that the formatting of the above code is pretty much s#!t, but why would one have to link to a source, @Brad? (My code's source is mostly my mind. Ain't that linkable...) – Johannes Pille Aug 1 '14 at 0:51
  • Brad, I took the code from here: http://stackoverflow.com/a/12845351 and then butchered it until it served my purposes. I am not a coder by profession. – user57465 Aug 1 '14 at 2:04
1

Short answer: Add comments_template(); under your the_content().

But there's a better way to do this that will carry over any formatting and style you have on your main blog. Maybe you don't want this, though, so ignore the rest if needed.

Here's the improved loop code:

$args = array( 'numberposts' => -1);
$query = new WP_Query($args);

if ($query->have_posts()) {

    while ($query->have_posts()) {
        $query->the_post();
        get_template_part('content', get_post_format());
    }

    wp_reset_postdata();
} else {
    get_template_part('content', 'none');
}

That is a pretty basic Wordpress loop, nothing really tricky going on and you almost had exactly that with your provided code. Instead of directly outputting the content, this code will load the template parts just like the loop in index.php, which should include the call to comments_template() if its a half-way decent theme.

Here's the updated improved loop that should work for older themes that do not have content.php:

if ($query->have_posts()) {

    while ($query->have_posts()) {
        $query->the_post();
        the_content();
        comments_template();
    }

    wp_reset_postdata();
} else {
    echo 'No posts found.';
}

But your original code is literally doing the same thing, just organized a bit differently. I would suggest adding a wp_reset_postdata() call to your code, though, so you can restore the original query. Probably won't hurt to leave it out, but it'll be a mighty hard bug to track down in the future.

  • Your short answer worked great! But, on the other hand, your improved code gave me a page with blog header but no body. Code typo? Your short answer is good enough for me, but if you want to trouble shoot your improved code I will try out any fixes. Otherwise, I'll go with your short answer. – user57465 Aug 1 '14 at 1:43
  • That probably means your theme doesn't use "template parts," i.e. there is no content.php or content-[POST_TYPE].php. Its a relatively new addition to Wordpress, so older themes do not use it. No biggie, though, as you can replace my get_template_part() function call with the_content(); comments_template(); and it should work as expected. – The Maniac Aug 1 '14 at 1:57
  • I should have mentioned that I was working with the Wordpress Twenty-ten theme. Your code for older themes did indeed work. Many thanks for your kind help. – user57465 Aug 1 '14 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.