Wordpress - 自定义 Post 类型混乱 - 分类不显示

Wordpress - Custom Post Type mess up - taxonomy not showing

我为 Person 设置了自定义 post 类型和分类法,效果很好。我添加了很多 post,直到我决定将名称更改为 People。我添加的 posts 消失了,所以我不得不将 posts 重新分配给正确的 post 类型。

我现在遇到的问题是分类法没有显示(位置和工作职能)。它们似乎没有注册,也没有显示在菜单或自定义类型 post 页面上。

我已经尝试重置永久链接并使用 flush_rewrite_rules();但仍然没有。有人可以帮忙吗?

<?php   
/**
 * People post type & taxonomies
 *
 * Post Type:   people
 * Taxonomies:  function
 *            
 */
       
add_action( 'init', 'create_people_post_type', 5 );
function create_people_post_type() {            
    flush_rewrite_rules();
    register_post_type( 'People',
        [
            'labels' => [
                'name' => __( 'People' ),
                'singular_name' => __( 'People' ),
            ],
            'public' => true,
            'has_archive' => true,
            'supports' => ['title', 'thumbnail', 'author' ],
        ]
    );
    
    register_taxonomy( 'job-functions',
        'People',
        [
            'labels' => [
                'name' => __( 'Job Functions' ),
                'singular_name' => __( 'Job Function' ),
            ],
            'hierarchical' => true,
            'show_admin_column' => true,
        ]
    );
}
    
add_action('init','add_locations_to_people');
function add_locations_to_people(){
    register_taxonomy_for_object_type('location', 'People');
}

/**
 * Location post type 
 *
 * Post Type:   location
 *             
 */
add_action( 'init', 'create_office_location_post_type', 4 );
function create_office_location_post_type() {
    register_post_type( 'location',
        [
            'labels' => [
                'name' => __( 'Office Locations' ),
                'singular_name' => __( 'Office Location' ),
            ],
            'public' => true,
            'has_archive' => true,
            'supports' => [ 'title', 'author' ],
        ]
    );
}

当您注册新的 post 类型时,您需要设置它的分类法。

register_post_type( 'People',
        [
            'labels' => [
                'name' => __( 'People' ),
                'singular_name' => __( 'People' ),
            ],
            'public' => true,
            'taxonomies'=>array('job-functions'),
            'has_archive' => true,
            'supports' => ['title', 'thumbnail', 'author' ],
        ]
    );

更新:

这是您案例的最终代码。在您的第一个给定代码中,您首先注册 post 类型,然后尝试将分类法添加到已注册的 post 类型。所以,你只需要更换他们的地方。

add_action( 'init', 'create_people_post_type', 5 );
function create_people_post_type() {
    flush_rewrite_rules();
    register_taxonomy(
        'job-functions',
        'People',
        [
            'labels' => [
                'name' => __( 'Job Functions' ),
                'singular_name' => __( 'Job Function' ),
            ],
            'hierarchical' => true,
            'show_admin_column' => true,
        ]
    );
    register_post_type( 'People',
        [
            'labels' => [
                'name' => __( 'People' ),
                'singular_name' => __( 'People' ),
            ],
            'public' => true,
             'taxonomies'=>array('job-functions'),
            'has_archive' => true,
            'supports' => ['title', 'thumbnail', 'author' ],
        ]
    );
}
add_action('init','add_locations_to_people');
function add_locations_to_people(){
    register_taxonomy_for_object_type('location', 'People');
}

UPD2:

add_action( 'init', 'create_people_post_type', 5 );
function create_people_post_type() {
    flush_rewrite_rules();
    register_taxonomy(
        'job-functions',
        'People',
        [
            'labels' => [
                'name' => __( 'Job Functions' ),
                'singular_name' => __( 'Job Function' ),
            ],
            'hierarchical' => true,
            'show_admin_column' => true,
        ]
    );

    register_taxonomy(
        'location',
        'People',
        [
            'labels' => [
                'name' => __( 'Location' ),
                'singular_name' => __( 'Location' ),
            ],
            'hierarchical' => true, //true if it is category like, false if it is  tag like
            'show_admin_column' => true,
        ]
    );
    register_post_type( 'People',
        [
            'labels' => [
                'name' => __( 'People' ),
                'singular_name' => __( 'People' ),
            ],
            'public' => true,
             'taxonomies'=>array('job-functions','location'),
            'has_archive' => true,
            'supports' => ['title', 'thumbnail', 'author' ],
        ]
    );
}