Buat widget untuk menampilkan komentar di WordPress

<?php
  add_action( 'widgets_init', 'bb_load_widgets' );
 
function bb_load_widgets() {
    register_widget( 'bb_comments_Widget' );
}
 
class bb_comments_Widget extends WP_Widget {
 
    /**
     * Widget setup.
     */
    function bb_comments_Widget() {
        /* Widget settings. */
        $widget_ops = array( 'classname' => 'bb_comments_Widget', 'description' => __('A widget that displays comments', 'bb_comments_Widget') );
 
        /* Widget control settings. */
        $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'bb_comments_Widget' );
 
        /* Create the widget. */
        $this->WP_Widget( 'bb_comments_Widget', __('GS bookshowcase reviews', 'bb_comments_Widget'), $widget_ops, $control_ops );
    }
 
    /**
     * How to display the widget on the screen.
     */
    function widget( $args, $instance ) {
        extract( $args );
 
        /* Our variables from the widget settings. */
        $title = apply_filters('widget_title', $instance['title'] );
        $number = $instance['number'];
 
        /* Before widget (defined by themes). */
        echo $before_widget;
 
        /* Display the widget title if one was input (before and after defined by themes). */
        if ( $title )
            echo '<h3 class="widget-title">' . $title . '</h4>';
 
        /* Get recent comments */
        $args = array(   
    'post_type' => 'gs_bookshowcase',
    'number' => $number,
    'orderby' => 'date',
    'order' => 'DESC'
  );

$comments = get_comments($args);
echo '<ol class="has-avatars has-dates has-excerpts wp-block-latest-comments">';
foreach($comments as $comment) :
                    
        ?>
         <li class="wp-block-latest-comments__comment">
                <?php echo wp_kses_post( get_avatar( $comment,$size = '60', '', '', array('class' => 'wp-block-latest-comments__comment-avatar') ) ); ?>
                <article><footer class="wp-block-latest-comments__comment-meta">
                    <a class="wp-block-latest-comments__comment-author" href="<?php echo get_comment_author_url();?>"><?php echo $comment->comment_author  ?></a> on <a class="wp-block-latest-comments__comment-link" href="<?php echo get_permalink($comment->comment_post_ID);?>#comment-<?php echo $comment->comment_ID; ?>"><?php echo get_the_title($comment->comment_post_ID); ?></a><time datetime="2022-02-28T19:42:47+08:00" class="wp-block-latest-comments__comment-date"><?php echo $comment->comment_date  ?></time></footer><div class="wp-block-latest-comments__comment-excerpt"><p><?php echo strip_tags($comment->comment_content); ?></p>
</div></article></li>



        <?php 
        endforeach;
        echo '</ol>';
 
        /* After widget (defined by themes). */
        echo $after_widget;
    }
 
    /**
     * Update the widget settings.
     */
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
 
        /* Strip tags for title and name to remove HTML (important for text inputs). */
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['number'] = strip_tags( $new_instance['number'] );
 
        return $instance;
    }
 
 
    function form( $instance ) {
 
        /* Set up some default widget settings. */
        $defaults = array( 'title' => __('GS Bookshowcase Reviews'), 'number' => __('5'));
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>
 
        <!-- Widget Title: Text Input -->
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'hybrid'); ?></label>
            <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
        </p>
 
        <!-- Number of posts -->
        <p>
            <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e('Number of reviews to show:'); ?></label>
            <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $instance['number']; ?>" size="3" />
        </p>
    <?php
    }
}
Loreto