php - Wordpress - 自定义帖子类型搞砸了 - 分类不显示

标签 php wordpress custom-post-type taxonomy

我为 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/

相关文章:

php - 在 Woocommerce 中更改删除购物车项目 url

php - 使用 MySQL 和 PHP 启动帖子线程的逻辑?

wordpress - 如何更改 WordPress 中的页面链接

php - 基于复选框的多个sql条件

javascript - js 弹出窗口仅加载第一篇文章的内容,而不是单个文章

php - 根据当前查询变量在 Wordpress 中预过滤 get_next_post()

php - 如何让 PHP 5.4.40 支持 LDAP 5.4?

php - 如何将 javascript 添加到 Zend Framework 中的表单?

php - 在 Wordpress 中加载更多帖子 Ajax 按钮 - 修复

php - Wordpress:WP_Query 如何应用具有自定义帖子类型的搜索条件