CPT、自定义用户角色和用户管理器问题

时间:2014-08-19 作者:skifast

我已使用以下代码创建自定义用户角色:

    /* aggiungi ruolo */
add_role(\'brokers\', \'Brokers\', array(
\'read\' => true, // True allows that capability
\'edit_pages\' => true,
\'edit_others_pages\' => true,
\'edit_published_pages\' => true,
\'publish_pages\' => true,
));
然后我用这个代码创建了两个CPT

// Sinistri
add_action( \'init\', \'register_cpt_sinistri\' );
function register_cpt_sinistri() {
$labels = array( 
    \'name\' => _x( \'Sinistri\', \'sinistri\' ),
    \'singular_name\' => _x( \'Sinistro\', \'sinistri\' ),
    \'add_new\' => _x( \'Aggiungi Sinistro\', \'sinistri\' ),
    \'add_new_item\' => _x( \'Aggiungi Nuovo Sinistro\', \'sinistri\' ),
    \'edit_item\' => _x( \'Modifica Sinistro\', \'sinistri\' ),
    \'new_item\' => _x( \'Nuovo Sinistro\', \'sinistri\' ),
    \'view_item\' => _x( \'Guarda Sinistro\', \'sinistri\' ),
    \'search_items\' => _x( \'Cerca Sinistri\', \'sinistri\' ),
    \'not_found\' => _x( \'Nessun Sinistro Trovato\', \'sinistri\' ),
    \'not_found_in_trash\' => _x( \'Sinistri non trovati\', \'sinistri\' ),
    \'parent_item_colon\' => _x( \'Parent Sinistro:\', \'sinistri\' ),
    \'menu_name\' => _x( \'Gestione Sinistri\', \'gallery\' ),
);
$args = array( 
    \'labels\' => $labels,
    \'hierarchical\' => true,
    \'description\' => \'Image galleries for teachers classes\',
    \'supports\' => array( \'title\', \'editor\', \'author\'),
    \'public\' => true,
    \'show_ui\' => true,
    \'show_in_menu\' => true,
    \'show_in_nav_menus\' => true,
    \'publicly_queryable\' => true,
    \'exclude_from_search\' => true,
    \'has_archive\' => false,
    \'query_var\' => true,
    \'can_export\' => true,
    \'rewrite\' => true,
    \'capability_type\' => \'post\',
    \'capabilities\' => array(
        \'edit_post\' => \'edit_sinistri\',
        \'edit_posts\' => \'edit_sinistris\',
        \'edit_others_posts\' => \'edit_other_sinistris\',
        \'publish_posts\' => \'publish_sinistris\',
        \'read_post\' => \'read_sinistri\',
        \'read_private_posts\' => \'read_private_sinistris\',
        \'delete_post\' => \'delete_sinistri\'
    ),
    // as pointed out by iEmanuele, adding map_meta_cap will map the meta correctly 
    \'map_meta_cap\' => true
);
register_post_type( \'sinistri\', $args );
flush_rewrite_rules();
}
此代码:

//Area Clienti Riservata
add_action( \'init\', \'register_cpt_area_clienti\' );
function register_cpt_area_clienti() {
    $labels = array( 
        \'name\' => _x( \'Area Clienti\', \'area_clienti\' ),
        \'singular_name\' => _x( \'Area Clienti\', \'area_clienti\' ),
        \'add_new\' => _x( \'Add New\', \'area_clienti\' ),
        \'add_new_item\' => _x( \'Add New Area Clienti\', \'area_clienti\' ),
        \'edit_item\' => _x( \'Edit Area Clienti\', \'area_clienti\' ),
        \'new_item\' => _x( \'New Area Clienti\', \'area_clienti\' ),
        \'view_item\' => _x( \'View Area Clienti\', \'area_clienti\' ),
        \'search_items\' => _x( \'Search Area Clienti\', \'area_clienti\' ),
        \'not_found\' => _x( \'No area clienti found\', \'area_clienti\' ),
        \'not_found_in_trash\' => _x( \'No area clienti found in Trash\', \'area_clienti\' ),
        \'parent_item_colon\' => _x( \'Parent Area Clienti:\', \'area_clienti\' ),
        \'menu_name\' => _x( \'Area Clienti\', \'area_clienti\' ),
    );
    $args = array( 
        \'labels\' => $labels,
        \'hierarchical\' => false,
        \'description\' => \'Gestione clienti agenzia assicurativa\',
        \'supports\' => array( \'title\', \'editor\' ),

        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'menu_position\' => 5,

        \'show_in_nav_menus\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => true,
        \'has_archive\' => false,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => true,
        \'capabilities\' => array(
        \'edit_post\' => \'edit_sinistri\',
        \'edit_posts\' => \'edit_sinistris\',
        \'edit_others_posts\' => \'edit_other_sinistris\',
        \'publish_posts\' => \'publish_sinistris\',
        \'read_post\' => \'read_sinistri\',
        \'read_private_posts\' => \'read_private_sinistris\',
        \'delete_post\' => \'delete_sinistri\'
    ),
    );
    register_post_type( \'area_clienti\', $args );
    flush_rewrite_rules();
}
然后,我用以下代码设置用户角色:

function add_theme_caps() {
    // gets the administrator role
    $roles = get_role( \'brokers\');
    $roles->add_cap( \'edit_sinistri\' ); 
    $roles->add_cap( \'edit_sinistris\' ); 
    $roles->add_cap( \'edit_other_sinistris\' ); 
    $roles->add_cap( \'publish_sinistris\' ); 
    $roles->add_cap( \'read_sinistri\' ); 
    $roles->add_cap( \'read_private_sinistris\' ); 
    $roles->add_cap( \'delete_sinistri\' ); 

    $admins = get_role( \'brokers\',\'administrator\' );
    $admins->add_cap( \'edit_area_clienti\' ); 
    $admins->add_cap( \'edit_area_clientis\' ); 
    $admins->add_cap( \'edit_other_area_clientis\' ); 
    $admins->add_cap( \'publish_area_clientis\' ); 
    $admins->add_cap( \'read_area_clienti\' ); 
    $admins->add_cap( \'read_private_area_clientis\' ); 
    $admins->add_cap( \'delete_area_clienti\' ); 
}
add_action( \'admin_init\', \'add_theme_caps\');
但我有个问题。我只能看到具有代理角色的CPT,而不能看到具有管理员角色的CPT。如何查看具有代理和管理员角色的两个CPT?谢谢

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

$admins = get_role( \'brokers\',\'administrator\' ); 部分没有意义。

get_role() 只接受一个角色名称,并且只返回一个角色。您需要分别对这两个角色进行这些更改。

结束

相关推荐