Latest comments from each category?

On the “home”-page I’m trying to display the most recently commented-on post for each category.

I managed to display the most recently published post for each category, but if an older post from each category gets a comments, that post should be bumped to the top of the list with that latest comment displayed.

Read More

Here’s the code I tried:

<?php $cat_args=array(
  'child_of'=>1,
  'exclude'=> 12,
  'orderby' => 'name',
  'order' => 'ASC'
   );
$categories=get_categories($cat_args);
  foreach($categories as $category) {
    $args=array(
      'showposts' => 1,
      'category__in' => array($category->term_id),
      'caller_get_posts'=>1
    );
    $posts=get_posts($args);
      if ($posts) {
        echo '<div class="pffcat"><h2><a href="' . get_category_link( $category->term_id ) . '">' . $category->name.'</a></h2> ';
        foreach($posts as $post) {
          setup_postdata($post); ?>


            <?php $recent_reviews = get_comments('post_id='.$post->ID.'&status=approve&number=1');?>

             <?php
                  foreach($recent_reviews as $recent_review) {
                    $comment_url = get_permalink($recent_review->comment_post_ID).'#reviews';
                    $comment_info = get_comment_meta($recent_review->comment_ID, 'comment_info', true);
                    $comment_rating = get_comment_avg_rating($comment_info);
                ?>

                <div id="post" class="post">
                  <div class="entry-summary">
                    <p><strong>Restaurant: <a href="<?php echo get_permalink($recent_review->comment_post_ID); ?>"><?php echo get_the_title($recent_review->comment_post_ID); ?></a></strong></p>
                    <p><?php echo get_limit_content(strip_tags($recent_review->comment_content), 150); ?></p>
                    <p><a href="<?php echo $comment_url; ?>" class="more-link">read review</a></p>
                  </div><!-- .entry-summary -->
                </div><!-- #post-## -->
                <div class="searchbox-r-home">
                  <img alt="" src="<?php bloginfo('template_directory'); ?>/images/star-m-<?php echo $comment_rating; ?>.png" />
                  <span><?php echo date('m/d/Y', strtotime($recent_review->comment_date)); ?></span>
                </div>
                <?php } // foreach($recent_reviews as $recent_review) { ?>
          <?php
        } 
        echo '</div><!--end box -->';// foreach($posts
      } // if ($posts
    } // foreach($categories
?>

Related posts

Leave a Reply