简体   繁体   中英

get_terms on custom post type/taxonomy returns nothing

I created a custom post type and custom taxonomy...

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');

Afterwards I created multiple multiple categories and tags. I'd like to retrieve the categories but when running the following, it doesn't return anything...

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

Any assistance would be greatly appreciated.

I was tried with your code & working fine. [ experiment's screenshot: http://imgur.com/a/jPms7 ]

Please make sure that you have created at-least one post under post-type 'deals' & assign some deals category with it.

[ or you can create the post type & taxonomies newly in standard way from here: https://generatewp.com/post-type/ ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM