【问题标题】:How to stop WordPress comments widget conflicting with theme comments如何阻止 WordPress 评论小部件与主题评论冲突
【发布时间】:2012-09-27 01:42:31
【问题描述】:

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

显示 cmets 的函数在一个类中。

我遇到的问题是,每当我显示此小部件时,它都会与为我的 WordPress 主题返回的 cmets 数量混淆或冲突。

示例。在小部件中选择显示 5 cmets。在网站上的一个页面上,我有一个包含 8 个 cmets 的帖子。启用小部件时,仅显示这 8 个 cmets 中的 6 个。

如果我禁用小部件,所有 cmets 都会显示。

这是显示cmets的功能

/**
     * 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'); ?>

起初我以为是 Gravatar 导致了冲突,但我删除了它,它并没有做出任何改变。

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

编辑: 似乎是 get_comment_link() 导致了问题。

如果我删除该函数的两个实例,则调用小部件和 cmets 显示正常。 我试过了:wp_reset_postdata();wp_reset_query();rewind_posts();都没有效果。

【问题讨论】:

    标签: wordpress widget comments conflict


    【解决方案1】:

    尝试在foreach 循环之后添加对wp_reset_query() 的调用。

    http://codex.wordpress.org/Function_Reference/wp_reset_query

    【讨论】:

    • 我确实尝试了 wp_reset_query 但它没有解决问题。
    • 也许你想要rewind_posts()codex.wordpress.org/Function_Reference/rewind_posts 显示 8 个 cmets 中的 6 个是没有意义的,但也许您的意思是 6 到 8?
    • 我知道这听起来很奇怪,但事情就是这样发生的。看一看:wpinsite.com/articles/…。现在 8 个 cmets 中有 7 个正在显示。
    • 我的猜测是它不包含在您的示例代码中...我看到一些未包含的内容,例如从未使用过的 global $comments 和未使用过的 $this-&gt;hook已定义。
    • 是的,抱歉,$this->hook 我已移除。我拿了我正在使用的函数的这段代码。我在课堂上定义了 $this->hook。从我的调查来看,这似乎与这个电话有关:esc_url(get_comment_link($comment->comment_ID))
    猜你喜欢
    • 2013-10-18
    • 1970-01-01
    • 2014-03-29
    • 2012-05-28
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    相关资源
    最近更新 更多