Php Wordpress-自定义帖子类型混乱-未显示分类法

Php Wordpress-自定义帖子类型混乱-未显示分类法,php,wordpress,custom-post-type,taxonomy,Php,Wordpress,Custom Post Type,Taxonomy,我为Person设置了一个自定义的帖子类型和分类法,效果很好。我添加了不少帖子,直到我决定把名字改成人物。我添加的帖子消失了,所以我不得不将帖子重新分配到正确的帖子类型 我现在的问题是没有显示分类法(位置和作业功能)。它们似乎没有注册,也没有显示在菜单或自定义类型的帖子页面上 我尝试过重置永久链接,并使用了flush_rewrite_rules();但还是什么都没有。有人能帮忙吗 <?php /** * People post type & taxonomies * * P

我为
Person
设置了一个自定义的帖子类型和分类法,效果很好。我添加了不少帖子,直到我决定把名字改成
人物
。我添加的帖子消失了,所以我不得不将帖子重新分配到正确的帖子类型

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

我尝试过重置永久链接,并使用了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');
}

当您注册新的帖子类型时,您需要设置它将具有哪些分类法

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

这是您案例的最终代码。在第一个给定的代码中,首先注册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:


嗨,看来我们已经完成了一半,因为“工作职能”正在显示,但“地点”没有。我曾尝试将“位置”添加到分类法数组中,但没有效果。我认为这很简单,您可以自己添加。好的,检查我的UPD2。对于位置,您不需要额外的添加操作。它可以在一个功能中完成。位置分类法基于现有的“位置”自定义帖子类型。您所做的只是创建一个称为位置的新分类法,但这不会使用fI已经创建的位置作为CPT。希望这是有道理的。在我更改CPT名称之前,我上面的原始代码已经工作了,所以我仍然不明白它是如何停止工作的?您网站的代码的哪一部分为另一个自定义帖子类型创建了位置分类法?知道这一点很重要。因为优先级队列。如果您不需要注册位置l,因为它已经存在,那么我们应该知道它是如何存在的以及在哪里存在的。“添加位置到人员”功能运行的优先级为10,创建“人员”类型运行的优先级为5。我们需要知道什么是优先级位置寄存器函数OK,我尝试过修改优先级,但仍然没有成功。上面添加的代码
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');
}
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' ],
        ]
    );
}