我为 Person
设置了自定义帖子类型和分类法,效果很好。在我决定将名称更改为 People
之前,我添加了很多帖子。我添加的帖子消失了,所以我不得不将这些帖子重新分配给正确的帖子类型。
我现在遇到的问题是分类法没有显示(位置和工作职能)。它们似乎没有注册,也没有显示在菜单或自定义类型的帖子页面上。
我已经尝试重置永久链接并使用 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' ],
]
);
}
最佳答案
当您注册新的帖子类型时,您需要设置它的分类法。
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', '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' ],
]
);
}
关于php - Wordpress - 自定义帖子类型搞砸了 - 分类不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46981566/