【问题标题】:Using a custom taxonomy, is there a way to show_ui for Admins only?使用自定义分类法,有没有办法只为管理员显示_ui?
【发布时间】:2013-09-28 18:45:05
【问题描述】:

我有一个自定义分类法,如果当前用户是管理员,有没有办法只在仪表板中显示它?我需要一些类似的东西,但它不起作用:

 global $current_user;
 global $showui;
     if($current_user->roles[0] == 'administrator') {
       $showui = true;
     }  
     else {
           $showui = false;
          }
//Custom Industry Taxonomy Code (For Projects CPT)
register_taxonomy('servicecategory',
    array ( 0 => 'servicecategory',),
    array( 'hierarchical' => true, 
            'label' => 'Specialties',
            'show_ui' => $showui,
            'query_var' => true,
            'rewrite' => true,
            'show_in_nav_menus' => true,
            'show_admin_column' => true,                
            'singular_label' => 'Menu'
        ) 
    );  

【问题讨论】:

    标签: wordpress taxonomy role


    【解决方案1】:

    您可以首先命名管理此分类的能力。然后将功能添加到管理员,如下所示:

    register_taxonomy('servicecategory',
        array ( 0 => 'servicecategory',),
        array( 'hierarchical' => true, 
                'label' => 'Specialties',
                'show_ui' => true,
                'query_var' => true,
                'rewrite' => true,
                'show_in_nav_menus' => true,
                'show_admin_column' => true,                
                'singular_label' => 'Menu',
                'capabilities' => array ( //giving a name to the capability
                    'manage_terms' => 'manage_servicecategory', 
                    'edit_terms' => 'manage_servicecategory',
                    'delete_terms' => 'manage_servicecategory',
                    'assign_terms' => 'manage_servicecategory'
                )
        ) 
    ); 
    $role = get_role('administrator');
    $role->add_cap("manage_servicecategory");
    

    【讨论】:

    • 这解决了只允许管理员编辑的情况,但是你知道有一种方法可以对非管理员完全隐藏吗?
    • @TripsLeft 最后这件事你想清楚了吗? (对非管理员完全隐藏)。也很想知道答案:)
    【解决方案2】:

    未经测试,但尝试添加此仅显示给管理员

    'show_ui' => current_user_can( 'update_core' )
    

    这将 ui 限制为仅具有更新 wordpress 功能的用户,这些用户始终是管理员。

    【讨论】:

      猜你喜欢
      • 2020-05-18
      • 2021-06-04
      • 2020-08-13
      • 2021-02-15
      • 2023-01-26
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      • 1970-01-01
      相关资源
      最近更新 更多