2016-09-19 86 views
0

我創建了一個自定義後的類型和自定義分類...get_terms沒有返回

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

任何援助將不勝感激。

+0

真的往往只是有這些條款沒有職位。 WordPress默認情況下不會返回沒有帖子的條款。 – Stratboy

回答

0

我試了你的代碼&工作正常。 [實驗的屏幕截圖:http://imgur.com/a/jPms7]

請確保您已創建至少一個帖子在帖子類型'交易'&指定一些交易類別與它。

[或者你可以&分類新標準的方式從這裏創建的帖子類型:https://generatewp.com/post-type/]

+1

我發現添加第二個參數get_terms固定它,以及當沒有帖子存在時... get_terms('deals-category',array('hide_empty'=> 0)) – BradB