如何停止评论小工具与主题评论冲突

时间:2012-09-26 作者:Jason

我有一个小部件,可以检索和显示WordPress站点的最新评论。它显示注释作者、Gravatar、注释和日期/时间。

显示注释的函数位于类中。

我遇到的问题是,每当我显示这个小部件时,它都会弄乱或与为我的WordPress主题返回的评论数量冲突。

实例在小部件中,选择显示5条注释。在该网站的一个页面上,我有一篇有8条评论的帖子。启用小部件时,仅显示这8条注释中的6条。

如果禁用小部件,将显示所有注释。

这是显示注释的功能

/**
     * Retrieves the latest comments
     *
     * Shows a list of latest comments ordered by the date added
     *
     * @param int $limit - The number of posts to display
     * @param int $chars - The number of characters to display for the post body
     * @param int $size - Size of the comment Gravatar
     * @param boolean $displayCommentsIcon - Whether to display the comment Gravatar
     *
     */
     public function aaw_get_latest_comments($display_comments_icon = true, $comments_icon_size = 50, $comments_amount = 5, $comments_chars = 35, $display_comments_date = true) {
        global $comments;

        $com_excerpt = \'\';

        $aaw_comments = get_comments(array(\'number\' => $comments_amount, \'status\' => \'approve\'));

        if($aaw_comments){
            foreach((array)$aaw_comments as $aaw_comment){
                if($comments_chars > 0) {
                    $com_excerpt = self::aaw_snippet_text($aaw_comment->comment_content, $comments_chars);
                }

                echo \'<li>\';

                    if($display_comments_icon == \'true\'){
                        echo \'<a href="\'.esc_url(get_comment_link($aaw_comment->comment_ID) ).\'" title="\'. __(\'Commented on: \', $this->hook). $aaw_comment->post_title.\'">\';
                        echo get_avatar($aaw_comment, $comments_icon_size);
                        echo \'</a>\';
                    }
                    echo \'<div class="aaw_info">\';
                    echo \'<a href="\'.esc_url(get_comment_link($aaw_comment->comment_ID) ).\'" title="\'. __(\'Commented on: \', $this->hook). $aaw_comment->post_title.\'">\';
                        echo \'<i>\'.strip_tags($aaw_comment->comment_author).\'</i>: \'.strip_tags($com_excerpt).\'...\';
                    echo \'</a>\';
                    if($display_comments_date == \'true\'){
                        echo \'<span class="aaw_meta">\'.get_comment_date(\'j M Y\',$aaw_comment->comment_ID).\' \'.__(\'at\', $this->hook).\' \'.get_comment_date(\'g:i a\',$aaw_comment->comment_ID).\'</span>\';
                    }
                    echo \'</div>\';
                echo \'</li>\';

            }
        } else {
            echo \'<li>\'.__(\'No comments available\', $this->hook).\'</li>\'."\\n";
        }


    }
这是我调用函数的方式:

<?php $advanced_activity_widget->aaw_get_latest_comments($display_comments_icon == \'true\' ? \'true\' : \'false\', $comments_icon_size, $comments_amount, $comments_chars, $display_comments_date == \'true\' ? \'true\' : \'false\'); ?>
起初我以为是格雷瓦塔引起了冲突,但我把它删除了,它没有改变。

任何帮助都将不胜感激。谢谢

1 个回复
SO网友:Pontus Abrahamsson

您可以这样做:

<?php $comments = get_comments(\'status=approve&number=5\'); ?>
<ul>
    <?php foreach ($comments as $comment) { ?>
        <li>
            <?php echo get_avatar( $comment, \'35\' ); ?>
            <a href="<?php echo get_permalink($comment->ID); ?>#comment-<?php echo $comment->comment_ID; ?>" title="on <?php echo $comment->post_title; ?>"> <?php echo strip_tags($comment->comment_author); ?>: <?php echo wp_html_excerpt( $comment->comment_content, 35 ); ?>... </a>
        </li>
    <?php }  ?>
</ul>

Ok, here’s some code explanations:

  • $comments = get_comments(\'status=approve&number=5\') ->获取5条最新评论get_avatar( $comment, \'35\' ) ->获取并显示35x35px大小的头像get_permalink($comment->ID)$comment->comment_ID -> 获取评论链接strip_tags($comment->comment_author) -> 显示评论作者wp_html_excerpt( $comment->comment_content, 35 ) -> 显示最多35个字符的注释

结束