Support » Fixing WordPress » get_comments() by Post ID

  • Resolved LarryRyan0824


    I am trying to get comments to show by their post ID. Right now this is displaying all of the recent comments. I feel like I am close. Can I do something along the lines of changing post_id=#id to post_id=$featuredPosts?

    function slide() {
    	global $post;
    	$featuredPosts = new WP_Query();
    	$comments = get_comments('number=2&status=approve&post_id=$id');
            <?php while ($featuredPosts->have_posts()) : $featuredPosts->the_post();?>
                        	<div class="slide_post_info">
                        	<h4 class="slide_post_title"><?php the_title(); ?></h4>
                            <?php the_excerpt(); ?>
                            <div class="slide_post_reactions">
                                <ul class="slide_post_meta">
                                    <li class="slide_post_commentcount"><a href="<?php the_permalink() ?>#disqus_thread"></a></li>
                                    <li class="bottomlisplit"></li>
                                    <li class="slide_post_time"><?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?></li>
                            <div class="slide_post_comments">
                            foreach($comments as $comm) :
    				echo '<li>';
    			        echo ':';
    				echo '</li>';
                        <?php endwhile; ?><!--/close loop-->
Viewing 2 replies - 1 through 2 (of 2 total)
  • Move the ‘$comments = …’ to the while loop.

    <?php while ($featuredPosts->have_posts()) : $featuredPosts->the_post();?>
    <?php $comments = get_comments( array( 'number' => 2, 'post_id' => get_the_ID() ) ); ?>

    Alright, it worked. Thanks, buddy.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_comments() by Post ID’ is closed to new replies.