过滤来自WordPress管理面板的回复评论

时间:2013-06-04 作者:David Reis

有没有办法从评论列表中筛选回复中的评论?我基本上只想在一个单独的过滤器上看到深度1的注释。

@GhostToast Ive添加了此选项,以在管理评论面板上创建一个新的过滤器“Ask”。有没有一种方法可以将顶级注释过滤器代码应用于此Ask过滤器?这将是非常好的,因为所有评论过滤器仍然有效,并且通过只显示顶级评论来过滤提问。。。


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

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

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

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

/**
 * Only display comments of specific post_id
 */ 
function wpse_63422_list_comments_from_specific_post( $clauses )
{
    $clauses->query_vars[\'post_id\'] = 12;
}

/**
 * Add link to specific post comments with counter
 */
function wpse_63422_new_comments_page_link( $status_links )
{
    $count = get_comments( \'post_id=12&count=1\' );

    if( isset( $_GET[\'ask\'] ) ) 
    {
        $status_links[\'all\'] = \'All\';
        $status_links[\'ask\'] = \'Ask (\'.$count.\')\';
    } 
    else 
    {
        $status_links[\'ask\'] = \'Ask (\'.$count.\')\';
    }

    return $status_links;
}

1 个回复
最合适的回答,由SO网友:GhostToast 整理而成

在注释循环中,跳过子项:

if( !empty( $comment->comment_parent ) ) { 
    //if the comment has a parent, skip it as it\'s not level 1
    continue;
}
编辑:上面的例子只在阅读循环中有用,下面的例子应该在管理方面有用:

add_filter(\'the_comments\', \'top_level_comments_filter\');

function top_level_comments_filter($comments){
    global $pagenow;
    if($pagenow == \'edit-comments.php\'){
        foreach($comments as $key => $value){
            if( !empty( $value->comment_parent ) ) { 
                unset($comments[$key]);
                continue;
            }
        }
    }
    return $comments;
}
编辑:根据您对原始问题的补遗,这将是您将所有内容组合在一起的方式。我对您的代码进行了一些清理,并为函数提供了一个内聚的名称空间。这使filter 我告诉你ask 正在设置的查询变量:

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

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

    // Check if our Query Var is defined    
    if( isset( $_GET[\'ask\'] ) ) {
        add_action( \'pre_get_comments\', \'dreis_list_comments_from_specific_post\', 10, 1 );
        add_filter( \'the_comments\', \'dreis_top_level_comments_filter\' );
    }

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

/**
 * Only display comments of specific post_id
 */ 
function dreis_list_comments_from_specific_post( $clauses ) {
    $clauses->query_vars[\'post_id\'] = 12;
}

/**
 * Add link to specific post comments with counter
 */
function dreis_new_comments_page_link( $status_links ) {
    $count = get_comments( \'post_id=12&count=1\' );

    if( isset( $_GET[\'ask\'] ) ) {
        $status_links[\'all\'] = \'All\';
        $status_links[\'ask\'] = \'Ask (\'.$count.\')\';
    } else {
        $status_links[\'ask\'] = \'Ask (\'.$count.\')\';
    }

    return $status_links;
}

/**
 * Remove non-top-level comments
 */
function dreis_top_level_comments_filter($comments){
    global $pagenow;
    if($pagenow == \'edit-comments.php\'){
        foreach($comments as $key => $value){
            if( !empty( $value->comment_parent ) ) { 
                unset($comments[$key]);
            continue;
            }
        }
    }
    return $comments;
}

结束

相关推荐

custom field in admin columns

我正在试图了解如何将自定义字段incr\\u编号放入admin列。然而,我无法理解,我在谷歌上搜索,但找不到直接的答案。这不会那么难吧? <?php /*-----------------------------------------------------------------------------------*/ /* WooThemes supportpress Custom Post Types Init */ /*--