修改后端的帖子列表

时间:2011-12-16 作者:I-M-JM

我想修改后端的帖子列表?

比方说,对于一个具有角色author的特定用户,O希望在posts列表中只列出少数帖子。我该怎么做?

1 个回复
SO网友:Bainternet

您可以使用edit_posts_per_page 挂钩或edit_{$post_type}_per_page 页面和自定义帖子类型的挂钩

您可以根据用户角色进行更改:

function my_edit_post_per_page_role_based( $per_page, $post_type ) {
    //admin sees 25 posts per page
    if (current_user_can(\'administrator\')){return 25;}
    //editor sees 20 posts per page
    elseif(current_user_can(\'editor\')){return 15;}
    //author sees 10 posts per page
    elseif(current_user_can(\'author\')){return 10;}
    //contributor sees 5 posts per page
    elseif(current_user_can(\'contributor\')){return 5;}
    return $per_page;
}

add_filter( \'edit_posts_per_page\', \'my_edit_post_per_page_role_based\' );
或根据用户id进行更改:

function my_edit_post_per_page_user_based( $per_page, $post_type ) {
    global $current_user;
    get_currentuserinfo();
    //specific user id ex: user with the ID of 23
    if (23 == $current_user->ID){
        return 20;
    }
    //user group
    $can_see_only_10 = array(2,5,36,55,47); //array holding the group ids
    if (in_array($current_user->ID,$can_see_only_10)){
        return 10;
    }

    return $per_page;
}

add_filter( \'edit_posts_per_page\', \'my_edit_post_per_page_user_based\' );

结束