我已创建自定义帖子类型listings
并为其定义了一些功能。但当添加功能时,它不起作用。
自定义帖子
register_post_type(\'listings\', array(
\'labels\' => array(
\'name\' => __(\'Restaurant listing\', \'thefoody\') ,
\'singular_name\' => __(\'Restaurant listing\', \'thefoody\') ,
\'add_new\' => __(\'Add New\', \'thefoody\') ,
\'add_new_item\' => __(\'Add New Restaurant listing\', \'thefoody\') ,
\'edit\' => __(\'Edit\', \'thefoody\') ,
\'edit_item\' => __(\'Edit Restaurant listing\', \'thefoody\') ,
\'new_item\' => __(\'New Restaurant listing\', \'thefoody\') ,
\'view\' => __(\'View Restaurant listing\', \'thefoody\') ,
\'view_item\' => __(\'View Restaurant listing\', \'thefoody\') ,
\'search_items\' => __(\'Search Restaurant listings\', \'thefoody\') ,
\'not_found\' => __(\'No Restaurant listings found\', \'thefoody\') ,
\'not_found_in_trash\' => __(\'No Restaurant listings found in Trash\', \'thefoody\') ,
\'parent\' => __(\'Parent Restaurant listing\', \'thefoody\') ,
) ,
\'public\' => true,
\'show_ui\' => true,
\'exclude_from_search\' => true,
\'hierarchical\' => true,
\'supports\' => array(
\'title\',
\'editor\',
\'thumbnail\'
) ,
\'query_var\' => true,
\'menu_icon\' => \'dashicons-megaphone\',
\'capability_type\' => \'listings\',
\'capabilities\' => array(
\'edit_post\' => \'edit_restaurant\',
\'edit_others_posts\' => \'edit_others_restaurants\',
\'publish_posts\' => \'publish_restaurants\',
\'read_post\' => \'read_restaurant\',
\'read_private_posts\' => \'read_private_restaurants\',
\'delete_posts\' => \'delete_restaurants\',
\'delete_others_posts\' => \'delete_others_restaurants\',
),
));
以及我的代码以添加功能:
function add_theme_caps() {
$role_shop_manager = get_role( \'shop_manager\' );
$role_shop_manager->add_cap(
\'edit_restaurants\',
\'publish_restaurants\',
\'view_posts\',
\'delete_post\',
\'delete_published_posts\',
\'delete_restaurant\'
);
$role_shop_manager->remove_cap( \'edit_others_restaurants\', \'edit_others_posts\' );
}
add_action( \'admin_init\', \'add_theme_caps\');
SO网友:Mukii kumar
add_action( \'init\', \'register_cpt_gallery\' );
function register_cpt_gallery() {
$labels = array(
\'name\' => _x( \'Galleries\', \'gallery\' ),
\'singular_name\' => _x( \'Gallery\', \'gallery\' ),
\'add_new\' => _x( \'Add New\', \'gallery\' ),
\'add_new_item\' => _x( \'Add New Gallery\', \'gallery\' ),
\'edit_item\' => _x( \'Edit Gallery\', \'gallery\' ),
\'new_item\' => _x( \'New Gallery\', \'gallery\' ),
\'view_item\' => _x( \'View Gallery\', \'gallery\' ),
\'search_items\' => _x( \'Search Galleries\', \'gallery\' ),
\'not_found\' => _x( \'No galleries found\', \'gallery\' ),
\'menu_name\' => _x( \'Galleries\', \'gallery\' ),
\'not_found_in_trash\' => _x( \'No galleries found in Trash\', \'gallery\' ),
\'parent_item_colon\' => _x( \'Parent Gallery:\', \'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,
\'menu_icon\' => get_bloginfo( \'template_url\' ) . \'/images/imagegallery.png\',
\'has_archive\' => true,
\'query_var\' => true,
\'can_export\' => true,
\'rewrite\' => true,
\'capabilities\' => array(
\'edit_post\' => \'edit_gallery\',
\'edit_posts\' => \'edit_galleries\',
\'edit_others_posts\' => \'edit_other_galleries\',
\'publish_posts\' => \'publish_galleries\',
\'read_post\' => \'read_gallery\',
\'read_private_posts\' => \'read_private_galleries\',
\'delete_post\' => \'delete_gallery\'
),
// as pointed out by iEmanuele, adding map_meta_cap will map the meta correctly
\'map_meta_cap\' => true,
\'show_in_nav_menus\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
);
register_post_type( \'gallery\', $args );
}
应将其他功能添加到角色中,以使权限在后端实际工作,包括“管理员”,例如:
function add_theme_caps() {
// gets the administrator role
$admins = get_role( \'administrator\' );
$admins->add_cap( \'edit_gallery\' );
$admins->add_cap( \'edit_galleries\' );
$admins->add_cap( \'edit_other_galleries\' );
$admins->add_cap( \'publish_galleries\' );
$admins->add_cap( \'read_gallery\' );
$admins->add_cap( \'read_private_galleries\' );
$admins->add_cap( \'delete_gallery\' );
}
add_action( \'admin_init\', \'add_theme_caps\');