To retrieve all posts from a category, while preserving the original Loop query, you create a new query like so:
<?php
$testimonials = new WP_Query(
array(
'post_status' => 'publish',
'post_type' => 'post',
'posts_per_page' => 30, // limit number of posts for which to get ALL the comments of
'category__in' => array( 2, 6, 18 ) // specify the categories you want the posts of
)
);
?>
The variable $testimonials
now contains all posts from the categories specified. You can place this inside the current main loop. In single.php
, that would look something like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); // this is the main loop, used in single.php will only contain a single post ?>
<article id="post-<?php $post->ID; ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); // this gets the posts' own content ?>
<?php $testimonials = new WP_Query(
array(
'post_status' => 'publish',
'post_type' => 'post',
'posts_per_page' => 30, // limit number of posts for which to get ALL the comments of
'category__in' => array( 2, 6, 18 ) // specify the categories you want the posts of
)
);
if ( $testimonial->have_posts() ) : while ( $testimonial->have_posts() ) : $testimonial->the_post();
// please note that $testimonial->the_post() has overwritten the original $post,
// but since we're on single.php and the main post content was already fetched that's probably alright
$comments = get_comments( array(
'status' => 'approve',
'post_id' => $post->ID,
'number' => '5', // if you want to limit the number of comments on that particular post ID
));
foreach( $comments as $comment ) :
echo( $comment->comment_author . '<br />' . $comment->comment_content );
endforeach;
endwhile; // this quits the $testimonials loop, not the main loop
endif; // end of $testimonials->have_posts()
?>
</article>
<?php
endwhile; // this quits the main loop
endif; // end of have_posts()
?>