如何将特定页面的评论与其他评论分开

时间:2012-11-22 作者:cmsdeployed

我在页面上使用WordPress评论作为联系方式。“我的联系人表单”页面中的注释设置为“自动批准”,并且仅对登录的管理员可见。现在,在仪表板中,我想将我的联系人表单评论与其他评论分开。我想把它们放在一个主题选项页面,或者在评论页面上使用一个过滤器。为什么我要用这种新方法?因为我相信它会更加人性化。

1 个回复
SO网友:brasofilo

基于此Answer, 这很容易。

Result:
comments from a single post

Code:
检查评论

// ID of the post/page that we want a separate list of comments
$the_id = \'1\';

add_action( \'current_screen\', \'wpse_73581_exclude_lazy_hook\' , 10, 2 );

/**
  * Delay hooking our clauses filter to ensure it\'s only applied when needed. 
  */
function wpse_73581_exclude_lazy_hook( $screen )
{
    if ( $screen->id != \'edit-comments\' )
        return;

    // Check if our Query Var is defined    
    if ( isset( $_GET[\'hello_world\'] ) )
        add_action( \'pre_get_comments\', \'wpse_73581_list_one_post_comments\', 10, 1 );

    add_filter( \'comment_status_links\',  \'wpse_73581_link_to_hello_world\' );
}


/**
 * Only display comments from specific post
 */
function wpse_73581_list_one_post_comments( $clauses )
{
    global $the_id;
    $clauses->query_vars[\'post_id\'] = $the_id;
}

/**
 * Add link to list of comments from specific post with counter
 */
function wpse_73581_link_to_hello_world( $status_links )
{
    global $wpdb, $the_id;

    // Get the number of comments from our post
    $count = count(
            $wpdb->get_results(
                    $wpdb->prepare(
                            "SELECT comment_ID FROM $wpdb->comments 
                            WHERE comment_post_ID = $the_id
                            AND comment_approved != \'trash\'", 
                            ARRAY_A
                    )
            )
    );

    // Showing our special page
    if ( isset( $_GET[\'hello_world\'] ) )
    {
        // So we can remove the "current" class that is applied to this link
        $status_links[\'all\'] =
                \'<a href="edit-comments.php?comment_status=all">\'
                . __( \'All\' )
                . \'</a>\';

        // Our link with "current" class and comments count
        $status_links[\'hello_world\'] =
                \'<a href="edit-comments.php?comment_status=all&hello_world=1" class="current" style="margin-leff:60px">\'
                . __( \'Hello World\' )
                . \' <span class="count">(\'
                . $count
                . \')</a></span>\';
    }
    // Showing other comments pages (All, Pending, Approved, etc)
    else
    {
        $status_links[\'hello_world\'] =
                \'<a href="edit-comments.php?comment_status=all&hello_world=1" style="margin-leff:60px">\'
                . __( \'Hello World\' )
                . \'  <span class="count">(\'
                . $count
                . \')</span></a>\';
    }

    return $status_links;
}

结束

相关推荐

Add_Filters()和Apply_Filter()有什么作用?

我有点困惑,为什么这不起作用-尽管必须说,我不太确定是什么apply_filters() 和add_filter 正在做,所以任何一般性的提示都会很好!我想要一个查询,在一个帖子页面上显示之前的五篇帖子。我正在发送当前帖子的日期,并希望应用一个过滤器,过滤出早于此的帖子。function sw_filter_posts_before( $where = \'\', $date) { $where .= \" AND post_date < \'\" . $date . \"