# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Comments](https://wpide.com/library/category/comments/)
- Tags: comments, shortcode, widgets
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/recent-comments-shortcode/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Recent Comments Shortcode" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
