skip to Main Content

I am trying to make a shortcode that should pull up the most recent comment (only one), Comment author name and thumbnail of the blog post on which this comment is published no matter if the blog post is the most recent or the older one. The comment should be the most recent one.

I made the code but it is not pulling the most recent comment.

function ct_comment_block(){
    
  $query = new WP_Query(
      array(
          'post_type' => 'post',
          'post_status' => 'publish',
          'posts_per_page' => 1,
          'tax_query' => array(
                  'taxonomy' => 'calendar_category',
                  'field' => 'slug'
              ),
          'order' => 'ASC',
          'orderby' => 'menu_order'
      )
  );
$str = '';
$thumb_id = get_post_thumbnail_id();

$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);`$thumb_url = $thumb_url_array[0];
while ($query->have_posts()):
$query->the_post();

$str .= '<div class="comment-main">
          <div class="comment-image-wrapper"><a href="'.get_the_permalink().'">'.get_the_post_thumbnail().'</a></div>
        <div class="comment-wrapper">
        <h3>'.comment_sender().'</h3>
        <h5>Traveler Hobbyist</h5>
        <p>'.real_body().'</p>
        
        
        </div>'; 
$str .= '</div>';


endwhile;

 
return $str;

}

add_shortcode('show_comment' , 'ct_comment_block');

2

Answers


  1. I have written most of the my anser in the code comments.

    But I would use a $wpdb query to fetch the newest comment, get the id of the assosiated post and work from there.

    Hopefully this makes sense to you.

    <?php
    add_shortcode('show_comment' , 'ct_comment_block'); 
    function ct_comment_block(){
        if(!is_admin()){
            global $wpdb;
            $query = "SELECT * from $wpdb->comments WHERE comment_approved= '1' ORDER BY comment_date DESC LIMIT 0 ,1";    // This shows the newest comment     
            $comments = $wpdb->get_results($query);
            $strToReturn = '';
            if ($comments) {
                $strToReturn .= '<div class="someSortOfWrappingClass">';
                foreach ($comments as $comment) {
                    //Use $comment->comment_post_ID for the post_id like so
                    $link = get_permalink($comment->comment_post_ID);
                    //but you can check what else is inside $comment - like $comment->comment_author_email etc.
                    //Simply gather your html here 
                } //End of foreach
            $strToReturn .= '</div>';
            } //End of IF
            return $strToReturn;
        } else {
            return;
        }
        
    }
    
    Login or Signup to reply.
  2. Try it may help you for get most recent comments

    $recentcomments = get_comments( array( 
        'number'      => 10, // fetch number of comments.
        'status'      => 'approve', //  status approved comments.
        'post_status' => 'publish' // post status published comments.
    ) );
    
    if ( $recentcomments ) {
        foreach ( (array) $recentcomments as $comment ) {
    
            echo '<a href="' . esc_url( get_comment_link( $comment ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a>';
    
        }
    } 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search