wordpress自带的近期评论小工具不会显示详细的评论内容,而且还会显示治理员的评论,觉得不是很好,只能本人解决一下。花了近一个早晨才解决好,次要用无理解小工具的原理上了,然而应用起来就十分简略了,只需简略的两个步骤。该小工具在wordpress 3.4.1版本上测试经过。先来个截图预览下:
1、制造小工具
代码一堆能够不必管它,间接将代码保留到wordpress的/wp-content/widgets/comments.php文件。
为什么放到这里呢?由于像主题、插件这些都是存在wp-content这个目录上面,小工具寄存在这里能够对立治理,也合乎wordpress目录规定。
<?php</p>
<p>/**
 * 承继WP_Widget_Recent_Comments
 * 这样就只要要重写widget办法就能够了
 */
class My_Widget_Recent_Comments extends WP_Widget_Recent_Comments {</p>
<p>    /**
     * 结构办法,次要是定义小工具的称号,引见
     */
    function My_Widget_Recent_Comments() {
        $widget_ops = array('classname' => 'my_widget_recent_comments', 'description' => __('显示**评论内容'));
        $this->WP_Widget('my-recent-comments', __('我的**评论', 'my'), $widget_ops);
    }</p>
<p>    /**
     * 小工具的渲染办法,这里就是输入评论
     */
    function widget($args, $instance) {
        global $wpdb, $comments, $comment;</p>
<p>        $cache = wp_cache_get('my_widget_recent_comments', 'widget');</p>
<p>        if (!is_array($cache))
            $cache = array();</p>
<p>        if (!isset($args['widget_id']))
            $args['widget_id'] = $this->id;</p>
<p>        if (isset($cache[$args['widget_id']])) {
            echo $cache[$args['widget_id']];
            return;
        }</p>
<p>        extract($args, EXTR_SKIP);
        $output = '';
        $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title'], $instance, $this->id_base);
        if (empty($instance['number']) || !$number = absint($instance['number']))
            $number = 5;
        //获取评论,过滤掉治理员本人
        $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE user_id !=2 and comment_approved = '1' and comment_type not in ('pingback','trackback') ORDER BY comment_date_gmt DESC LIMIT $number");
        $output .= $before_widget;
        if ($title)
            $output .= $before_title . $title . $after_title;</p>
<p>        $output .= '<ul id="myrecentcomments">';
        if ($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);</p>
<p>            foreach ((array) $comments as $comment) {
                //头像
                $avatar = get_avatar($comment, 40);
                //作者称号
                $author = get_comment_author();
                //评论内容
                $content = apply_filters('get_comment_text', $comment->comment_content);
                $content = mb_strimwidth(strip_tags($content), 0, '65', '...', 'UTF-8');
                $content = convert_smilies($content);
                //评论的文章
                $post = '<a href="' . esc_url(get_comment_link($comment->comment_ID)) . '">' . get_the_title($comment->comment_post_ID) . '</a>';</p>
<p>                //这里就是输入的html,能够依据需求自行修正
                $output .= '<li class="comment" style="padding-bottom: 5px; ">
            <p>
                <table class="tablayout"><tbody><tr>
                <td class="tdleft" style="width:55px;vertical-align:top;">' . $avatar . '</td>
                <td class="tdleft" style="vertical-align:top;">
                    <p class="comment-author"><strong><span class="fn">' . $author . '</span></strong> <span class="says">宣布在 ' . $post . '</span></p>
                </tr></tbody></table>
            </p>
            <p class="comment-content"><p class="last">' . $content . '</p>
</p>
        </li>';
            }
        }
        $output .= '</ul>';
        $output .= $after_widget;</p>
<p>        echo $output;
        $cache[$args['widget_id']] = $output;
        wp_cache_set('my_widget_recent_comments', $cache, 'widget');
    }</p>
<p>}</p>
<p>//注册小工具
register_widget('My_Widget_Recent_Comments');
在主标题录下的funtions.php文件中加载这个小工具:
require(get_template_directory().'/../../widgets/comments.php');
2、进入后盾治理拖入评论小工具
抉择外观->小工具,能够看到评论小工具就加载出去了,如下:
间接将评论小工具拖动到侧边栏就OK了。
小结
有的冤家可能会担忧代码呈现什么成绩,这个代码是从零碎自带的评论小工具中复制过去的,次要是修正评论的获取和评论的显示款式。零碎自带的评论小工具代码能够参考/wp-includes/default-widgets.php中的WP_Widget_Recent_Comments类。
以上就是安达网络工作室关于《自己做wordpress评论插件修改评论样式(两步美化评论内容)》的一些看法。更多内容请查看本栏目更多内容!
本文实例讲述了WordPress在SAE平台完成的动态化插件及用法。分享给大家供大家参考,详细如下: WordPress是...
复制代码代码如下: <?php //Put this in functions.php function get_primary_image($id, $size){ ...
小洞不补大洞享乐。关于bloggers来说这是永恒的真谛,仅仅花一点工夫在马上就晋级上省下了很多之后修复一些...
上面引见四种WordPress 显示文章摘要办法: 一、应用WordPress自带摘要性能 我目前另一WordPress博客就是应...
get_header()(获取头部) 引入主题的头部模板,默许会引入以后主标题录里的 header.php 文件。假如指定了一...
从ThemeForest购买的是正版商业主题AVADA(有售后技术支持),应用国际阿里云主机(配置不算低),网站大局...