首页 > 解决方案 > WordPress - 自定义分类法未显示在外观 > 菜单中

问题描述

我有一个名为Resources的自定义帖子类型:

register_post_type(
    'resources',
    build_post_args(
        'resources', 'Resource', 'Resources',
        array(
            'menu_icon'     => 'dashicons-welcome-write-blog',
            'menu_position' => 20,
            'has_archive' => true,
            'public'      => true,
            'supports' => array('editor', 'title','author','thumbnail', 'revisions'),
            'taxonomies' => array('sector', 'subject', 'type'),
        )
    )
);

我还创建了type分配给资源的分类法:

register_taxonomy(  
    'type', 
    'type', 
    array(  
        'hierarchical' => true,  
        'label' => 'Type',  
        'query_var' => true,
        'rewrite' => array(
            'slug' => 'type', 
            'with_front' => false 
        ) 
    )
); 

在 WordPress 后端,这里是我所有type的:

在此处输入图像描述

但是,当我转到外观 > 菜单 > 并单击类别下拉菜单时,仅显示以下选项:

在此处输入图像描述

为什么是这样?

标签: wordpresscustom-post-typecustom-taxonomy

解决方案


resources您好请添加以下代码并检查您在创建帖子类型时缺少很多东西。(注意:请删除您为创建resources帖子和type分类添加的所有代码)

function ro_resources_custom_post_type() {
    $labels = array(
        'name'                => __( 'Resources' ),
        'singular_name'       => __( 'Resource'),
        'menu_name'           => __( 'Resources'),
        'parent_item_colon'   => __( 'Parent Resource'),
        'all_items'           => __( 'All Resources'),
        'view_item'           => __( 'View Resource'),
        'add_new_item'        => __( 'Add New Resource'),
        'add_new'             => __( 'Add New'),
        'edit_item'           => __( 'Edit Resource'),
        'update_item'         => __( 'Update Resource'),
        'search_items'        => __( 'Search Resource'),
        'not_found'           => __( 'Not Found'),
        'not_found_in_trash'  => __( 'Not found in Trash')
    );
    $args = array(
        'label'               => __( 'resources'),
        'description'         => __( 'Best Resources'),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields'),
        'public'              => true,
        'hierarchical'        => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'has_archive'         => true,
        'can_export'          => true,
        'exclude_from_search' => false,
            'yarpp_support'       => true,
        'publicly_queryable'  => true,
        'capability_type'     => 'page'
);
    register_post_type( 'resources', $args );
}
add_action( 'init', 'ro_resources_custom_post_type', 0 );

add_action( 'init', 'ro_create_resources_custom_taxonomy', 0 );

//create a custom taxonomy name it "type" for your posts
function ro_create_resources_custom_taxonomy() {

  $labels = array(
    'name' => _x( 'Types', 'taxonomy general name' ),
    'singular_name' => _x( 'Type', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Types' ),
    'all_items' => __( 'All Types' ),
    'parent_item' => __( 'Parent Type' ),
    'parent_item_colon' => __( 'Parent Type:' ),
    'edit_item' => __( 'Edit Type' ), 
    'update_item' => __( 'Update Type' ),
    'add_new_item' => __( 'Add New Type' ),
    'new_item_name' => __( 'New Type Name' ),
    'menu_name' => __( 'Types' ),
  );    

  register_taxonomy('types',array('resources'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'type' ),
  ));
}

经过测试并且运行良好


推荐阅读