马春杰杰 Exit Reader Mode

WordPress添加“最新评论文章”的小工具

样式1:

在后台的外观–小工具处,增加一个“最新评论文章”的小工具,可以方便的在主页增加最新评论的文章~~下面是该工具的样式:

代码:

修改functions.php文件:

// 开始增加最新评论文章
class My_Recent_Comments_Widget extends WP_Widget {

    public function __construct() {
        parent::__construct(
            'my_recent_comments_widget', // Base ID
            '最新评论文章', // Name
            array( 'description' => '显示最新评论的文章' ) // Args
        );
    }

    public function widget( $args, $instance ) {
        global $wpdb;

        $number_of_comments = ! empty( $instance['number_of_comments'] ) ? $instance['number_of_comments'] : 5;
        $recent_comments_query = "
            SELECT DISTINCT comment_post_ID, MAX(comment_ID) as comment_ID 
            FROM $wpdb->comments 
            WHERE comment_approved = '1' 
            AND comment_type = '' 
            GROUP BY comment_post_ID 
            ORDER BY comment_ID DESC 
            LIMIT %d
        ";
        $recent_comments = $wpdb->get_results($wpdb->prepare($recent_comments_query, $number_of_comments));

        if ($recent_comments) {
            echo '<div class="widget widget_ui_posts" style="top: 0px;"><h3>最新评论文章</h3><ul class="nopic">';
            foreach ($recent_comments as $comment) {
                $comment_data = get_comment($comment->comment_ID);
                $post_id = $comment->comment_post_ID;
                $comment_date = mysql2date('Y年n月j日', $comment_data->comment_date);
                $comment_count = get_comments_number($post_id);

                echo '<li>';
                echo '<a target="_blank" href="' . esc_url(get_permalink($post_id)) . '">';
                echo '<span class="text">' . esc_html(get_the_title($post_id)) . '</span>';
                echo '<span class="muted">最后修改日期:' . $comment_date . '</span>';
                echo '<span class="muted">评论(' . $comment_count . ')</span>';
                echo '</a></li>';
            }
            echo '</ul></div>';
        }
    }

    public function form( $instance ) {
        $number_of_comments = ! empty( $instance['number_of_comments'] ) ? $instance['number_of_comments'] : 5;
        ?>
        <p>
        <label for="<?php echo esc_attr( $this->get_field_id( 'number_of_comments' ) ); ?>">显示评论数量:</label> 
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'number_of_comments' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number_of_comments' ) ); ?>" type="number" value="<?php echo esc_attr( $number_of_comments ); ?>">
        </p>
        <?php 
    }

    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['number_of_comments'] = ( ! empty( $new_instance['number_of_comments'] ) ) ? strip_tags( $new_instance['number_of_comments'] ) : '';

        return $instance;
    }

}

function register_my_recent_comments_widget() {
    register_widget( 'My_Recent_Comments_Widget' );
}

add_action( 'widgets_init', 'register_my_recent_comments_widget' );
// 结束增加最新评论文章

样式2:

如果我想美化一下,比如,当鼠标放到文章名字时,显示当前文章的评论,修改如下:

functions.php:

//开始修改最新评论文章
class My_Recent_Comments_Widget extends WP_Widget {

    public function __construct() {
        parent::__construct(
            'my_recent_comments_widget',
            '最新评论文章',
            array( 'description' => '显示最新评论的文章' )
        );
    }

    public function widget( $args, $instance ) {
        global $wpdb;

        $number_of_comments = ! empty( $instance['number_of_comments'] ) ? $instance['number_of_comments'] : 5;
        $recent_comments_query = "
            SELECT DISTINCT comment_post_ID, MAX(comment_ID) as comment_ID 
            FROM $wpdb->comments 
            WHERE comment_approved = '1' 
            AND comment_type = '' 
            GROUP BY comment_post_ID 
            ORDER BY comment_ID DESC 
            LIMIT %d
        ";
        $recent_comments = $wpdb->get_results($wpdb->prepare($recent_comments_query, $number_of_comments));

        if ($recent_comments) {
            echo '<div class="widget widget_ui_posts" style="top: 0px;"><h3>最新评论文章</h3><ul class="nopic">';
            foreach ($recent_comments as $comment) {
                $comment_data = get_comment($comment->comment_ID);
                $post_id = $comment->comment_post_ID;
                $comment_date = mysql2date('Y年n月j日', $comment_data->comment_date);
                $comment_count = get_comments_number($post_id);

                // // 获取第一个评论
                // $first_comment = get_comments(array(
                //     'post_id' => $post_id,
                //     'number'  => 1,
                //     'status'  => 'approve',
                // ))[0]->comment_content ?? '无评论';
                
                
                
                // 检查评论是否为私密
                $is_private = get_comment_meta($comment->comment_ID, '_private', true) == 'true';

                // 获取第一个评论,考虑到私密评论
                $first_comment = !$is_private ? get_comments(array(
                    'post_id' => $post_id,
                    'number'  => 1,
                    'status'  => 'approve',
                ))[0]->comment_content ?? '无评论' : '私密评论';
                
                
                

                echo '<li>';
                echo '<a target="_blank" href="' . esc_url(get_permalink($post_id)) . '">';
                echo '<span class="text">' . esc_html(get_the_title($post_id)) . '</span>';
                echo '<span class="muted">最后修改日期:' . $comment_date . '</span>';
                echo '<span class="muted">评论(' . $comment_count . ')</span>';
                // 添加隐藏的评论部分
                echo '<div class="first-comment" style="display:none;font-size:12px; color:#1a8e6a;">' . esc_html($first_comment) . '</div>';
                
                echo '</a></li>';
            }
            echo '</ul></div>';
        }
    }

    public function form( $instance ) {
        $number_of_comments = ! empty( $instance['number_of_comments'] ) ? $instance['number_of_comments'] : 5;
        ?>
        <p>
        <label for="<?php echo esc_attr( $this->get_field_id( 'number_of_comments' ) ); ?>">显示评论数量:</label> 
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'number_of_comments' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number_of_comments' ) ); ?>" type="number" value="<?php echo esc_attr( $number_of_comments ); ?>">
        </p>
        <?php 
    }

    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['number_of_comments'] = ( ! empty( $new_instance['number_of_comments'] ) ) ? strip_tags( $new_instance['number_of_comments'] ) : '';

        return $instance;
    }
}

function register_my_recent_comments_widget() {
    register_widget( 'My_Recent_Comments_Widget' );
}

add_action( 'widgets_init', 'register_my_recent_comments_widget' );
//结束修改最新评论文章

footer.php:

<script>
    document.addEventListener('DOMContentLoaded', function() {
        var links = document.querySelectorAll('.widget_ui_posts li a');

        links.forEach(function(link) {
            link.addEventListener('mouseover', function() {
                this.querySelector('.first-comment').style.display = 'block';
            });

            link.addEventListener('mouseout', function() {
                this.querySelector('.first-comment').style.display = 'none';
            });
        });
    });
</script>

效果:

样式3:

不显示博主的评论:

//开始修改最新评论文章
class My_Recent_Comments_Widget extends WP_Widget {

    public function __construct() {
        parent::__construct(
            'my_recent_comments_widget',
            '最新评论文章',
            array( 'description' => '显示最新评论的文章' )
        );
    }

    public function widget( $args, $instance ) {
        global $wpdb;

        $number_of_comments = ! empty( $instance['number_of_comments'] ) ? $instance['number_of_comments'] : 5;
        $admin_user_id = 1; 
        $recent_comments_query = "
            SELECT DISTINCT comment_post_ID, MAX(comment_ID) as comment_ID 
            FROM $wpdb->comments 
            WHERE comment_approved = '1' 
            AND comment_type = '' 
            AND user_id != %d
            GROUP BY comment_post_ID 
            ORDER BY comment_ID DESC 
            LIMIT %d
        ";
    $recent_comments = $wpdb->get_results($wpdb->prepare($recent_comments_query, $admin_user_id, $number_of_comments));

        if ($recent_comments) {
            echo '<div class="widget widget_ui_posts" style="top: 0px;"><h3>最新评论文章</h3><ul class="nopic">';
            foreach ($recent_comments as $comment) {
                $comment_data = get_comment($comment->comment_ID);
                $post_id = $comment->comment_post_ID;
                $comment_date = mysql2date('Y年n月j日', $comment_data->comment_date);
                $comment_count = get_comments_number($post_id);

                // // 获取第一个评论
                // $first_comment = get_comments(array(
                //     'post_id' => $post_id,
                //     'number'  => 1,
                //     'status'  => 'approve',
                // ))[0]->comment_content ?? '无评论';
                
                
                
                // 检查评论是否为私密
                $is_private = get_comment_meta($comment->comment_ID, '_private', true) == 'true';

                // 获取第一个评论,考虑到私密评论
                $first_comment = !$is_private ? get_comments(array(
                    'post_id' => $post_id,
                    'number'  => 1,
                    'status'  => 'approve',
                ))[0]->comment_content ?? '无评论' : '私密评论';
                
                
                

                echo '<li>';
                echo '<a target="_blank" href="' . esc_url(get_permalink($post_id)) . '">';
                echo '<span class="text">' . esc_html(get_the_title($post_id)) . '</span>';
                echo '<span class="muted">最后修改日期:' . $comment_date . '</span>';
                echo '<span class="muted">评论(' . $comment_count . ')</span>';
                // 添加隐藏的评论部分
                echo '<div class="first-comment" style="display:none;font-size:12px; color:#1a8e6a;">' . esc_html($first_comment) . '</div>';
                
                echo '</a></li>';
            }
            echo '</ul></div>';
        }
    }

    public function form( $instance ) {
        $number_of_comments = ! empty( $instance['number_of_comments'] ) ? $instance['number_of_comments'] : 5;
        ?>
        <p>
        <label for="<?php echo esc_attr( $this->get_field_id( 'number_of_comments' ) ); ?>">显示评论数量:</label> 
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'number_of_comments' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number_of_comments' ) ); ?>" type="number" value="<?php echo esc_attr( $number_of_comments ); ?>">
        </p>
        <?php 
    }

    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['number_of_comments'] = ( ! empty( $new_instance['number_of_comments'] ) ) ? strip_tags( $new_instance['number_of_comments'] ) : '';

        return $instance;
    }
}

function register_my_recent_comments_widget() {
    register_widget( 'My_Recent_Comments_Widget' );
}

add_action( 'widgets_init', 'register_my_recent_comments_widget' );
//结束修改最新评论文章