• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧
您的位置:首页 > CMS教程 >WordPress > WordPress自定义角色无法在自定义文章类型中使用标签和分类的问题

WordPress自定义角色无法在自定义文章类型中使用标签和分类的问题

作者:小兽 字体:[增加 减小] 来源:互联网 时间:2018-11-02

小兽向大家介绍了WordPress自定义角色无法在自定义文章类型中使用标签和分类的问题等相关知识,希望对您有所帮助

导语:作者在wordpress主题创建一个bloger的角色,创建了一个blog的文章类型,用于注册博客用户来写博客,但是遇到了一个问题,就是博客用户登录后,写文章无法使用分类和标签。

作者开始使用的是WordPress自带的分类法‘category’、‘post_tag’,自定义角色写自定义文章类型的文章时,无法使用分类和标签。叶子又使用自定义分类法定义了分类,也不能使用。经过再三研究代码,终于解决了。

自定义文章类型代码

作者自定义了文章类型博客,使用 capabilities 定义权限,注意,这一点很重要。

function my_custom_post_blog() {
    
    $labels = array(
    'name'                => _x( 'Blogs', 'Post Type General Name', 'whychinatours' ),
    'singular_name'       => _x( 'Blog', 'Post Type Singular Name', 'whychinatours' ),
    'menu_name'           => esc_html__( 'Blogs', 'whychinatours' ),
    'all_items'           => esc_html__( 'All Blogs', 'whychinatours' ),
    'view_item'           => esc_html__( 'View Blog', 'whychinatours' ),
    'add_new_item'        => esc_html__( 'Add New Blog', 'whychinatours' ),
    'add_new'             => esc_html__( 'New Blog', 'whychinatours' ),
    'edit_item'           => esc_html__( 'Edit Blog', 'whychinatours' ),
    'update_item'         => esc_html__( 'Update Blog', 'whychinatours' ),
    'search_items'        => esc_html__( 'Search Blogs', 'whychinatours' ),
    'not_found'           => esc_html__( 'No Blogs found', 'whychinatours' ),
    'not_found_in_trash'  => esc_html__( 'No Blogs found in Trash', 'whychinatours' ),
);
$args = array(
    'label'               => esc_html__( 'blog', 'whychinatours' ),
    'description'         => esc_html__( 'Blogs information pages', 'whychinatours' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'thumbnail', 'author','excerpt', 'comments'),
    'taxonomies'          => array(),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'blog',
    'capabilities' => array(  
        'edit_post' => 'edit_blog',   
        'edit_posts' => 'edit_blogs',   
        'edit_others_posts' => 'edit_others_blogs',   
        'publish_posts' => 'publish_blogs',   
        'read_post' => 'read_blog',   
        'read_private_posts' => 'read_private_blogs',   
        'delete_post' => 'delete_blog',   
    )  ,
    'rewrite' => array('slug' => 'blogs'),
    'query_var' => true,
);
 
register_post_type( 'blog', $args );	
}
add_action( 'init', 'my_custom_post_blog' );

自定义WordPress角色

WordPress本身有订阅者、投稿者、作者、编辑、管理员的角色,作者在这里定义了一个Bloger的角色,专门用来发布博客。

function Yct_Add_role() 
{   
    if (get_role('bloger')) {
        remove_role('bloger');
    }
    add_role(
        'bloger', 
        __('Bloger', 'whychinatours'),
        array(
            'read' => true, //read权限表示个人信息
            'read_blog' => true,
            'edit_blog'=> true,
            'edit_blogs'=> true,
            'publish_blogs'=> true,
            'delete_blog' => true,
            'upload_files' => true,
            'level_2' => true,
            'level_1' => true,
            'level_0' => true
        )
    );
}
add_action('init', 'Yct_Add_role');

注意,这个Bloger的角色,除了开始在自定义文章类型里面定义的权限之外,还是需要指定‘read’,这样,登录后才会自动跳转到后台页面,不然,登录后跳转页面会提示你没有权限访问该页,这个该页是指个人信息页。

当然,你也可以强行指定跳转路径到编辑Blog类型的文章上,这样的方式也是可以访问后台的。

另外,如果需要上传文件,就加上‘upload_files’。如果Bloger可以编辑其他人的文章,就加上‘edit_others_blogs’,这里叶子没有加。

自定义分类法

如果使用原生态的分类法,作者没有找出可以在写blog时使用原生态分类的方法,所以只能使用自定义分类法。

function my_taxonomies_blog() {
    $labels = array(
        'name'              => _x( 'Blog Tag', 'taxonomy 名称' , 'whychinatours'),
        'singular_name'     => _x( 'blog tag', 'taxonomy 单数名称', 'whychinatours' ),
        'search_items'      => __( 'search blog tag' , 'whychinatours'),
        'all_items'         => __( 'Blog tag' , 'whychinatours'),
        'parent_item'       => __( 'parent blog tag' , 'whychinatours'),
        'parent_item_colon' => __( 'parent blog tag' , 'whychinatours'),
        'edit_item'         => __( 'edit blog tag' , 'whychinatours'),
        'update_item'       => __( 'update blog tag' , 'whychinatours'),
        'add_new_item'      => __( 'add blog tag' , 'whychinatours'),
        'new_item_name'     => __( 'add blog tag' , 'whychinatours'),
        'menu_name'         => __( 'Blog tag' , 'whychinatours'),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => false,
        'capabilities' => array(  
            'manage_terms' => 'manage_categories',   
            'edit_terms' => 'manage_categories',   
            'delete_terms' => 'manage_categories',   
            'assign_terms' => 'edit_blogs'
        )  ,
    );
    register_taxonomy( 'blogs-tag', 'blog', $args );
 
}
 
add_action( 'init', 'my_taxonomies_blog', 0 );

注意,关键点在于注册分类法的时候,将分配标签的子权限‘assign_terms’指定到‘edit_blogs’,也就这一句:’assign_terms’ => ‘edit_blogs’

这样的话,自定义角色用户(Bloger)登录后,编辑自定义文章类型(blog)的文章,就是可以分配自定义分类法(blogs-tag)的标签了。

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

相关文章

  • 2018-11-02wordpress主题获取指定分类的子分类或子页面
  • 2018-11-02WordPress报error establishing a database connection错误
  • 2018-11-02给WordPress加个评论关闭时间提示
  • 2018-11-02WordPress网站速度慢,该如何检查原因?
  • 2017-05-13wordpress后台打开缓慢的解决方法(临时)
  • 2018-11-02WordPress中获取当前页面URL地址
  • 2017-05-13wordpress自定义摘要截取字数的代码
  • 2017-05-13WordPress文章ID不连续的解决方法
  • 2018-11-02wordpress企业建站选择Linux主机还是Windows主机?
  • 2017-05-13wp-Syntax wordpress高亮插件使用方法

文章分类

  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧

最近更新的内容

    • 非插件实现wordpress网站自动内链、外链
    • XShuan企业主题付费版与体验版的区别
    • wordpress如何在文章中插入视频
    • WordPress新手入门:分类目录和页面的区别
    • wordpress主题后门检测及漏洞修复技巧
    • wordpress教程之 WordPress 查看插件
    • wordpress通过当前文章的ID获取文章标题内容简介的信息
    • 自定义wordpress的登录页面
    • wordpress制作自定义菜单的方法
    • WordPress上传图片自动重命名的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有