WordPress基础教程

WordPress最新评论小工具插件:显示隐藏站长评论,显示评论数,自定义摘要长度

2018 年历史小工具代码归档;读者需在 Staging 自行验证兼容性,不作为商业插件承诺。

文章头图:WordPress最新评论小工具插件:显示隐藏站长评论,显示评论数,自定义摘要长度

历史代码归档(2018 年)

本文是 2018 年针对经典小工具(Classic Widgets)API 编写的代码归档,不作为现成插件维护。WordPress 5.8 之后默认使用块编辑器(Block Editor),小工具系统已迁移至 Block Widgets,此处代码需要搭配 Classic Widgets 插件才能正常使用。使用前请在 Staging 环境测试兼容性,不建议直接用于生产环境。

WordPress自带的近期评论插件无法显示评论内容,参考官网的小工具制作教程,自己仿照写了一个 — SuFob Recent Comments Display Plugin。

介绍

SuFob Recent Comments Display Plugin是一款WordPress最新评论显示小工具,通过这个小工具可以设置是否显示管理回复,设置摘要长度以及设置显示的评论数量。一个文件满足所需功能。

安装方法

  • 方法一(推荐)

在网站后台插件>安装插件,选择上传插件,手动上传安装并启用插件;也可以直接使用下方代码清单手动创建插件文件。

  • 方法二

在/当前主题/includes/widgets目录下建立sufob_widget_recent_comments.php文件,将代码清单的代码粘贴保存。在主题的function.php内添加下面的代码即可。

require_once( dirname(__FILE__ ) . '/includes/widgets/sufob-widget-comments.php' );

安装成功后在网站后台:外观>小工具 下会新增一个名为[SuFob.Com]最新评论小工具的新工具。

代码清单

'sufob_widget_recent_comments',

            // 后台小工具的描述

            'description' => __('显示你网站的最新评论') ,
            'customize_selective_refresh' => true,
        );
        parent::__construct('sufob-recent-comments', __('[SuFob.Com]最新评论小工具') , $widget_ops);
        $this->alt_option_name = 'sufob_widget_recent_comments';
    }

    public

    function widget($args, $instance)
    {
        global $wpdb;
        $cache = wp_cache_get(self::$cache_id, self::$cache_flag);
        if (!is_array($cache))
        {
            $cache = array();
        }

        if (isset($cache[$args['widget_id']]))
        {
            echo $cache[$args['widget_id']];
            return;
        }

        if (!isset($args['widget_id'])) $args['widget_id'] = $this->id;
        extract($args);
        $output = '';
        $title = (!empty($instance['title'])) ? $instance['title'] : __('最新评论');
        $title = apply_filters('widget_title', $title, $instance, $this->id_base);
        $number = (!empty($instance['number'])) ? absint($instance['number']) : 5;
        if (!$number) $number = 5;
        $excerpt = (!empty($instance['excerpt'])) ? absint($instance['excerpt']) : 35;
        if (!$excerpt) $excerpt = 35;
        /**
         * 参数过滤
         * 4.9.0 开始新增 `$instance` 参数
         */
        $exclude_post_author = empty($instance['show_post_author']) ? array(
            1
        ) : array();
        $comments = get_comments(apply_filters('widget_comments_args', array(
            'number' => $number,
            'post_author__not_in' => $exclude_post_author,
            'status' => 'approve',
            'post_status' => 'publish'
        ) , $instance));
        $output.= $args['before_widget'];
        if ($title)
        {
            $output.= $args['before_title'] . $title . $args['after_title'];
        }

        $output.= '';
        if (is_array($comments) && $comments)
        {

            // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)

            $post_ids = array_unique(wp_list_pluck($comments, 'comment_post_ID'));
            _prime_post_caches($post_ids, strpos(get_option('permalink_structure') , '%category%') , false);
            foreach((array)$comments as $comment)
            {
                $content = apply_filters('get_comment_text', $comment->comment_content);
                $content = mb_strimwidth(strip_tags($content) , 0, $excerpt, '...', 'UTF-8');
                $content = convert_smilies($content);
                $output.= '' . get_avatar($comment, 40);
                $output.= '';
                $output.= '' . get_comment_author($comment) . ' ';
                $output.= '在 《comment_ID)) . '" target="_blank">' . get_the_title($comment->comment_post_ID) . '》 评论:';
                $output.= '' . $content . '';
                $output.= '';
            }
        }

        $output.= '';
        $output.= $args['after_widget'];
        echo $output;
        $cache[$args['widget_id']] = $output;
        wp_cache_set(self::$cache_id, $cache, self::$cache_flag);
    }

    public

    function update($new_instance, $old_instance)
    {
        $instance = $old_instance;
        $instance['title'] = sanitize_text_field($new_instance['title']);
        $instance['number'] = absint($new_instance['number']);
        $instance['show_post_author'] = !empty($new_instance['show_post_author']);
        $instance['excerpt'] = absint($new_instance['excerpt']);
        $this->delete_comment_cache();
        return $instance;
    }

    public

    function form($instance)
    {
        $title = isset($instance['title']) ? $instance['title'] : '';
        $number = isset($instance['number']) ? absint($instance['number']) : 5;
        $show_post_author_check = (isset($instance['show_post_author']) && $instance['show_post_author'] === true) ? 'checked="checked"' : '';
        $excerpt = isset($instance['excerpt']) ? absint($instance['excerpt']) : 70;
?>
        get_field_id('title'); ?>">
        get_field_id('title'); ?>" name="get_field_name('title'); ?>" type="text" value="" />

        get_field_id('number'); ?>">
        get_field_id('number'); ?>" name="get_field_name('number'); ?>" type="number" step="1" min="1" value="" size="3" />

        get_field_id('show_post_author'); ?>">
        get_field_id('show_post_author'); ?>" name="get_field_name('show_post_author'); ?>" type="checkbox"  />

        get_field_id('excerpt'); ?>">
        get_field_id('excerpt'); ?>" name="get_field_name('excerpt'); ?>" type="text" value="" />

        <?php
    }

    /**
     * 刷新评论插件缓存
     */
    static
    function delete_comment_cache()
    {
        wp_cache_delete(self::$cache_id, self::$cache_flag);
    }
}

归档说明(弱化商业优先级)

这篇与插件代码来自 2018 年 WordPress 小工具实践,现作为 建站技术档案 保留:若你仍在使用经典小工具,请自行在 Staging 验证与 PHP 版本兼容性;新站可优先考虑 区块小工具 + 官方评论块 或其它维护活跃方案。

站内不再以评论插件作为主营方向;若你要解决的是 独立站追踪、SEO 或架构,见 增长与归因入口技术诊断

常见问题

问:代码片段直接可用吗?
答:需自行校对:上文片段为历史搬运,可能与当前 WordPress/PHP 不兼容,勿直接贴进生产。

问:为什么还要保留?
答:过程透明:展示早年如何用 Widget API 拼接需求;读者可对比今日最佳实践。

下一步

[2026 技术实战提示] 在真实的商业环境中执行上述策略时,请始终以官方最新文档的 API 参数或界面变动为准。建议配合 GTM Preview 和 Google Search Console 进行实时验证。

评论

留言需人工审核后才会显示;回复会随主评论一起发布。评论按文章独立归档,请在你阅读的那篇文章下留言。 技术诊断请发邮件 sue@sufob.com或查看联系说明