自己平时会经常修改主题,
偶尔会遇到需要添加 最近评论侧边栏
自己修改过wp自带的评论侧边栏,但是换主题后就要重新做
突然发现大发写过这个,特此记录

以下代码转自大发

class bigfa_widget3 extends WP_Widget {
    function bigfa_widget3() {
        $widget_ops = array('description' => '支持评论表情');
        $this->WP_Widget('bigfa_widget3', 'P-主题最新评论', $widget_ops);
    }
    function widget($args, $instance) {
        extract($args);
        $title = apply_filters('widget_title',esc_attr($instance['title']));
        $limit = strip_tags($instance['limit']);
        $email = strip_tags($instance['email']);
        echo $before_widget.$before_title.$title.$after_title;
        ?>
        <ul class="recentcomments">
            <?php
            global $wpdb;
            $limit_num = $limit ;
            $my_email ="'" . $email . "'";
            $rc_comms = $wpdb->get_results("SELECT ID, post_title, comment_ID, comment_author,comment_author_email,comment_date,comment_content FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID  = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' AND comment_author_email != $my_email ORDER BY comment_date_gmt DESC LIMIT $limit_num ");
            $rc_comments = '';foreach ($rc_comms as $rc_comm) { $rc_comments .= "<li>" . get_avatar($rc_comm,$size='30') ."<div class='rc-info'><span class='rc-reviewer'>".$rc_comm->comment_author."</span>在<a class='rc-post' href='". get_permalink($rc_comm->ID) . "#comment-" . $rc_comm->comment_ID. "'>" . $rc_comm->post_title .  "</a></div><div class='rc_excerpt'>".$rc_comm->comment_content."</div></li>\n";}
            $rc_comments = convert_smilies($rc_comments);
            echo $rc_comments;
            ?>
        </ul>


        <?php
        echo $after_widget;
    }
    function update($new_instance, $old_instance) {
        if (!isset($new_instance['submit'])) {
            return false;
        }
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['limit'] = strip_tags($new_instance['limit']);
        $instance['email'] = strip_tags($new_instance['email']);
        return $instance;
    }
    function form($instance) {
        global $wpdb;
        $instance = wp_parse_args((array) $instance, array('title'=> '', 'limit' => '', 'email' => ''));
        $title = esc_attr($instance['title']);
        $limit = strip_tags($instance['limit']);
        $email = strip_tags($instance['email']);
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">标题:<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('limit'); ?>">显示数量:(最好5个以下) <input class="widefat" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit; ?>" /></label>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('email'); ?>">输入你的邮箱以排除显示你的回复: <br>(留空则不排除)<input class="widefat" id="<?php echo $this->get_field_id('email'); ?>" name="<?php echo $this->get_field_name('email'); ?>" type="text" value="<?php echo $email; ?>" /></label>
        </p>
        <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
    <?php
    }
}
add_action('widgets_init', 'bigfa_widget3_init');
function bigfa_widget3_init() {
    register_widget('bigfa_widget3');
}
End
最后修改:2023 年 09 月 20 日
如果觉得我的文章对你有用,请随意赞赏