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
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.
Try it may help you for get most recent comments