自定义帖子类型/分类法上的get_terms不返回任何内容

时间:2016-09-19 14:45:31

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

之后我创建了多个多个类别和标签。我想检索类别,但在运行以下内容时,它不返回任何内容......

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

非常感谢任何协助。

1 个答案:

答案 0 :(得分:0)

我尝试使用您的代码&amp;工作正常。 [实验截图:http://imgur.com/a/jPms7]

请确保您在帖子类型'优惠'下创建了至少一个帖子&amp;用它来分配一些交易类别。

[或者您可以创建帖子类型&amp;新标准的分类法从这里开始:https://generatewp.com/post-type/]