WordPress仅显示1个自定义文章类型而非2个的问题求助
解决WordPress自定义文章类型(CPT)重复注册导致菜单消失的问题
嘿,我一眼就看到问题所在了——你在注册第二个自定义文章类型的时候,重复使用了第一个的slug Huren,WordPress会把这当成是对第一个CPT的覆盖操作,所以第一个就被替换掉了,后台菜单自然就只剩“覆盖后”的那个了。
具体错误点
看你custom_post_type2函数的最后一行:
register_post_type( 'Huren', $args );
这里的第一个参数应该是第二个CPT的唯一标识(slug),你写成了和第一个一样的Huren,正确的应该是Leasen(更建议用小写leasen,符合WordPress的命名规范,避免大小写兼容问题)。
修正后的完整代码
我还帮你优化了函数命名(更具语义)、调整了菜单位置(避免两个菜单挤在一起),以及把slug改成小写,代码如下:
/* Huren CPT */ function register_huren_post_type() { $labels = array( 'name' => _x( 'Huren', 'Post Type General Name', 'text_domain' ), 'singular_name' => _x( 'Huur', 'Post Type Singular Name', 'text_domain' ), 'menu_name' => __( 'Huren', 'text_domain' ), 'parent_item_colon' => __( 'Parent Item:', 'text_domain' ), 'all_items' => __( 'All Items', 'text_domain' ), 'view_item' => __( 'View Item', 'text_domain' ), 'add_new_item' => __( 'Add New Item', 'text_domain' ), 'add_new' => __( 'Add New', 'text_domain' ), 'edit_item' => __( 'Edit Item', 'text_domain' ), 'update_item' => __( 'Update Item', 'text_domain' ), 'search_items' => __( 'Search Item', 'text_domain' ), 'not_found' => __( 'Not found', 'text_domain' ), 'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ), ); $args = array( 'label' => __( 'Huren', 'text_domain' ), 'description' => __( 'Post Type voor huren', 'text_domain' ), 'labels' => $labels, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'page-attributes' ), 'taxonomies' => array( ), 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'show_in_admin_bar' => true, 'menu_position' => 5, // 第一个CPT放在菜单第5位 'menu_icon' => '', 'can_export' => true, 'has_archive' => false, 'exclude_from_search' => false, 'publicly_queryable' => true, 'capability_type' => 'page', ); register_post_type( 'huren', $args ); // 改成小写slug } add_action( 'init', 'register_huren_post_type', 0 ); /* Leasen CPT */ function register_leasen_post_type() { $labels = array( 'name' => _x( 'Leasen', 'Post Type General Name', 'text_domain' ), 'singular_name' => _x( 'Lease', 'Post Type Singular Name', 'text_domain' ), 'menu_name' => __( 'Leasen', 'text_domain' ), 'parent_item_colon' => __( 'Parent Item:', 'text_domain' ), 'all_items' => __( 'All Items', 'text_domain' ), 'view_item' => __( 'View Item', 'text_domain' ), 'add_new_item' => __( 'Add New Item', 'text_domain' ), 'add_new' => __( 'Add New', 'text_domain' ), 'edit_item' => __( 'Edit Item', 'text_domain' ), 'update_item' => __( 'Update Item', 'text_domain' ), 'search_items' => __( 'Search Item', 'text_domain' ), 'not_found' => __( 'Not found', 'text_domain' ), 'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ), ); $args = array( 'label' => __( 'Leasen', 'text_domain' ), 'description' => __( 'Post Type voor leasen', 'text_domain' ), 'labels' => $labels, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'page-attributes' ), 'taxonomies' => array( ), 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'show_in_admin_bar' => true, 'menu_position' => 6, // 第二个CPT放在第6位,避免和第一个重叠 'menu_icon' => '', 'can_export' => true, 'has_archive' => false, 'exclude_from_search' => false, 'publicly_queryable' => true, 'capability_type' => 'page', ); register_post_type( 'leasen', $args ); // 这里改成正确的slug } add_action( 'init', 'register_leasen_post_type', 0 );
额外注意事项
- 自定义文章类型的slug必须唯一,且建议使用小写字母+连字符的格式(比如
huren-items),避免和内置类型、插件冲突,也符合URL规范 - 函数名尽量语义化,不要用
custom_post_type2这种模糊的命名,方便后续维护 - 替换代码后,记得进入WordPress后台的「设置→固定链接」页面,直接点击「保存更改」(不需要修改任何设置),让WordPress更新重写规则,确保CPT的链接能正常访问
内容的提问来源于stack exchange,提问作者Max
相关产品推荐
相关产品推荐

