Support » Fixing WordPress » get_comments() by Post ID

  • Resolved LarryRyan0824

    (@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();
    	$featuredPosts->query("showposts=5&cat=15");
    	$comments = get_comments('number=2&status=approve&post_id=$id');
    
            <?php while ($featuredPosts->have_posts()) : $featuredPosts->the_post();?>
    
                        <div>
                        	<div class="slide_post_info">
                        	<h4 class="slide_post_title"><?php the_title(); ?></h4>
                            <?php the_excerpt(); ?>
                            </div>
                            <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>
                                </ul>
                            </div>
                            <div class="slide_post_comments">
                            <?php
                            foreach($comments as $comm) :
    				echo '<li>';
    				echo($comm->comment_author);
    			        echo ':';
    				echo($comm->comment_content);
    				echo '</li>';
    			endforeach;
    						?>
                            </div>
                        </div>
    
                        <?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.