You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为自定义文章类型Projects创建独立分类列表求助

为WordPress自定义文章类型Projects创建独立分类列表的解决方案

你当前的问题在于复用了WordPress默认的category分类法,导致Projects和普通文章共享同一套分类体系,无法实现独立分类列表。正确的做法是为Projects创建专属的自定义分类法,具体实现步骤如下:

1. 替换原有代码,注册专属自定义分类法+Projects自定义文章类型

删除你之前添加的两段代码,替换为以下完整代码(通过WP Code插件添加):

/*
* 注册Projects专属自定义分类法
*/
function register_project_taxonomy() {
    $labels = array(
        'name'              => _x( '项目分类', '分类法通用名称', 'twentytwentyone' ),
        'singular_name'     => _x( '项目分类', '分类法单数名称', 'twentytwentyone' ),
        'search_items'      => __( '搜索项目分类', 'twentytwentyone' ),
        'all_items'         => __( '所有项目分类', 'twentytwentyone' ),
        'parent_item'       => __( '父级项目分类', 'twentytwentyone' ),
        'parent_item_colon' => __( '父级项目分类:', 'twentytwentyone' ),
        'edit_item'         => __( '编辑项目分类', 'twentytwentyone' ),
        'update_item'       => __( '更新项目分类', 'twentytwentyone' ),
        'add_new_item'      => __( '添加新项目分类', 'twentytwentyone' ),
        'new_item_name'     => __( '新项目分类名称', 'twentytwentyone' ),
        'menu_name'         => __( '项目分类', 'twentytwentyone' ),
    );

    $args = array(
        'labels'            => $labels,
        'hierarchical'      => true, // 设为true则支持层级分类(和默认category逻辑一致),false为标签式
        'public'            => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'show_in_nav_menus' => true,
        'show_tagcloud'     => true,
        'show_in_rest'      => true, // 支持古腾堡编辑器
        'rewrite'           => array( 'slug' => 'project-category' ), // 分类的URL别名
    );

    register_taxonomy( 'project_categories', array( 'projects' ), $args );
}
add_action( 'init', 'register_project_taxonomy', 0 );

/*
* 注册Projects自定义文章类型
*/
function custom_post_type() {
    $labels = array(
        'name'                => _x( 'Projects', '文章类型通用名称', 'twentytwentyone' ),
        'singular_name'       => _x( 'Project', '文章类型单数名称', 'twentytwentyone' ),
        'menu_name'           => __( 'Projects', 'twentytwentyone' ),
        'parent_item_colon'   => __( '父级项目', 'twentytwentyone' ),
        'all_items'           => __( '所有项目', 'twentytwentyone' ),
        'view_item'           => __( '查看项目', 'twentytwentyone' ),
        'add_new_item'        => __( '添加新项目', 'twentytwentyone' ),
        'add_new'             => __( '添加新项目', 'twentytwentyone' ),
        'edit_item'           => __( '编辑项目', 'twentytwentyone' ),
        'update_item'         => __( '更新项目', 'twentytwentyone' ),
        'search_items'        => __( '搜索项目', 'twentytwentyone' ),
        'not_found'           => __( '未找到', 'twentytwentyone' ),
        'not_found_in_trash'  => __( '回收站中未找到', 'twentytwentyone' ),
    );
      
    $args = array(
        'label'               => __( 'projects', 'twentytwentyone' ),
        'description'         => __( '所有项目', 'twentytwentyone' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields' ),
        'taxonomies'          => array( 'project_categories' ), // 关联专属分类法
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest'        => true,
    );
      
    register_post_type( 'projects', $args );
}
add_action( 'init', 'custom_post_type', 0 );

2. 代码说明

  • register_project_taxonomy函数:为Projects创建名为project_categories的专属分类法,hierarchical参数控制是否支持上下级层级,按需调整即可。
  • 自定义文章类型的taxonomies参数:指定关联专属分类法,彻底和默认category体系切割,保证分类独立。
  • rewrite参数:自定义分类的URL别名,优化前台访问路径的可读性与SEO效果。

3. 效果验证

添加代码后进入WordPress后台:

  • 左侧菜单「Projects」下会新增「项目分类」选项,与普通文章的「分类」完全分离。
  • 编辑Projects文章时,侧边栏仅显示「项目分类」 metabox,不会出现普通文章的分类选项。
  • 前台访问分类归档页面时,路径为你的域名/project-category/分类别名/,仅展示该分类下的Projects文章。

内容的提问来源于stack exchange,提问作者Shahzad Iftikhar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 08:24:54