Wordpress - 自定义帖子类型搞乱 - 分类法没有显示

时间:2017-10-27 18:44:16

标签: 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');
}

}

<?php
/**
 * 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' ],
            ]
        );
    }

    ?>

1 个答案:

答案 0 :(得分:1)

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

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

UPD:

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

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' ],
        ]
    );
}