- First, go to your WordPress dashboard.
- From the dashboard, navigate to "Appearance" and then click on "Widgets."
- Look for the "Text" widget and drag it to your desired widget area, such as the sidebar.
- Open the "Text" widget and paste the following code to display comments:
<?php
$args = array(
'number' => '5', // number of comments to display
'status' => 'approve', // only approved comments to display
'post_id' => $post->ID, // current post ID
);
$comments = get_comments($args);
if($comments){
echo '<ul>';
foreach($comments as $comment){
echo '<li>';
echo '<a href="' . get_comment_link($comment) . '">';
echo get_the_title($comment->comment_post_ID);
echo '</a><br />';
echo $comment->comment_content;
echo '</li>';
}
echo '</ul>';
} else {
echo 'No comments yet.';
}
?>
- Click "Save" to save the changes, and the comments should now be displayed in the widget area you selected.