get_terms 自定义 post type/taxonomy returns 无

get_terms on custom post type/taxonomy returns nothing

我创建了自定义 post 类型和自定义分类...

function create_deals_post_type() {
    register_post_type('Deals',
        array(
            'labels' => array (
                'name'                  => __('Deals'),
                'singular_name'         => __('Deal'),
                'add_new'               => __('Add New'),
                'add_new_item'          => __('Add a new Deal'),
                'view_item'             => __('View Deal'),
                'edit_item'             => __('Edit Deal'),
                'new_item'              => __('New Deal'),
                'all_items'             => __('All Deals'),
                'search_items'          => __('Search Deals'),
                'not_found'             => __('No deals found'),
                'not_found_in_trash'    => __('No deals found in the trash'),
                'parent_item_colon'     => '',
                'menu_name'             => 'Deals'
            ),
            'can_export'            => true,
            'description'           => 'Delicious Deals',
            'public'                => true,
            'has_archive'           => true,
            'rewrite'               => apply_filters('et_project_posttype_rewrite_args', array(
                'feeds'         => true,
                'slug'          => 'deals',
                'with_front'    => false,)),
            'capability_type'       => 'post',
            'hierarchical'          => false,
            'show_ui'               => true,
            'show_in_menu'          => true,
            'show_in_nav_menus'     => true,
            'show_in_admin_bar'     => true,
            'menu_position'         => 0,
            'supports'              => array('title', 'editor', 'thumbnail')
        )
    );

    $labels = array(
        'name'              => _x('Categories', 'Deal category name', 'Divi'),
        'singular_name'     => _x('Category', 'Deal category singular name', 'Divi'),
        'search_items'      => __('Search Categories', 'Divi'),
        'all_items'         => __('All Categories', 'Divi'),
        'parent_item'       => __('Parent Category', 'Divi'),
        'parent_item_colon' => __('Parent Category:', 'Divi'),
        'edit_item'         => __('Edit Category', 'Divi'),
        'update_item'       => __('Update Category', 'Divi'),
        'add_new_item'      => __('Add New Category', 'Divi'),
        'new_item_name'     => __('New Category Name', 'Divi'),
        'menu_name'         => __('Categories', 'Divi'),
    );

    register_taxonomy('deals-category', 'deals', array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,)
    );

    $labels = array(
        'name'              => _x('Tags', 'Deal Tag name', 'Divi'),
        'singular_name'     => _x('Tag', 'Deal tag singular name', 'Divi'),
        'search_items'      => __('Search Tags', 'Divi'),
        'all_items'         => __('All Tags', 'Divi'),
        'parent_item'       => __('Parent Tag', 'Divi'),
        'parent_item_colon' => __('Parent Tag:', 'Divi'),
        'edit_item'         => __('Edit Tag', 'Divi'),
        'update_item'       => __('Update Tag', 'Divi'),
        'add_new_item'      => __('Add New Tag', 'Divi'),
        'new_item_name'     => __('New Tag Name', 'Divi'),
        'menu_name'         => __('Tags', 'Divi'),
    );

    register_taxonomy('deals_tag', 'deals', array(
        'hierarchical'      => false,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,)
    );
}

add_action('init', 'create_deals_post_type');

之后我创建了多个类别和标签。我想检索类别,但是当 运行 以下内容时,它 return 什么都没有...

$category_array = get_terms('deals-category');
foreach ($category_array as $item) {
    echo 'Item: ' . $item->name . '. Slug: ' . $item->slug . '</br>';
}

如有任何帮助,我们将不胜感激。

我试过你的代码并且工作正常。 [实验截图:http://imgur.com/a/jPms7]

请确保您已在 post 类型 'deals' 下创建至少一个 post 并为其分配一些交易类别。

[ 或者您可以从此处以标准方式新创建 post 类型和分类法:https://generatewp.com/post-type/ ]