Recent Comments Shortcode
Adds a [wpide_recent_comments] shortcode that lists the latest approved comments with the commenter's name, post title and a short excerpt.
Opens WPIDE on
Code
add_shortcode( 'wpide_recent_comments', function ( $atts ) {
$atts = shortcode_atts( array( 'number' => 5, 'words' => 15 ), $atts, 'wpide_recent_comments' );
$comments = get_comments( array(
'number' => min( 20, absint( $atts['number'] ) ),
'status' => 'approve',
'type' => 'comment',
'post_status' => 'publish',
) );
if ( ! $comments ) {
return '';
}
$html = '<ul class="wpide-recent-comments">';
foreach ( $comments as $comment ) {
$html .= sprintf(
'<li><strong>%1$s</strong> on <a href="%2$s">%3$s</a><br><span>%4$s</span></li>',
esc_html( get_comment_author( $comment ) ),
esc_url( get_comment_link( $comment ) ),
esc_html( get_the_title( $comment->comment_post_ID ) ),
esc_html( wp_trim_words( wp_strip_all_tags( $comment->comment_content ), absint( $atts['words'] ) ) )
);
}
$html .= '</ul>';
return $html;
} );
Notes
Options: [wpide_recent_comments number="8" words="20"].
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.