前面剛建了給 WordPress 創建新的文章類型函數:register_post_type() 該函數還有個 taxonomies 參數,用來給自定義文章類型添加分類法制止,給 WordPress 添加默認的分類和標籤支持方法很簡單, 只需要在創建文章類型的時候設置 taxonomies 參數如下:

  1. 'taxonomies'=> array('category','post_tag'),  

效果如圖:

然後這在實際中的用法很少,一般情況下我們使用新的分類法,比如我們要做一個企業站,給產品做了一個新的文章類型 product 。我們要給產品歸類,可以按類別分,比如電腦、手機。還可以按產地分,比如:國產山寨、水貨,還可以按價格區間分,比如:1000 元以下、 1000 到 3000,等等,我們就需要同時有 3 個分類法:類別、產地、價格區間。

在本部分教程的第一篇中我們也提到了,在 wp-includes/post.php 文件中 WordPress 使用 register_taxonomy 函數創建了分類-category 和標籤-post_tag 兩個分類法。

下面是 register_taxonomy 函數的參數好用法:

  1. <?php   
  2. register_taxonomy($taxonomy$object_type$args);   
  3. //$taxonomy 字符串型,必需,分類法的名稱,用英文哦   
  4. //$object_type 數組或字符串,必需,分類法所對應的文章類型   
  5. //$args--可選,配置參數   
  6. ?>   

$args 參數是個數組,跟 register_post_type 函數的 $args 參數類似,詳細:
label-
labels-數組:

  • 'name'
  • 'singular_name'
  • 'search_items'
  • 'popular_items'
  • 'all_items'
  • 'parent_item'
  • .... 略

public-
show_in_nav_menus-是否在菜單設置頁面顯示
.... 略,參考 register_post_type 函數

實例,在前面的一篇文章中,我們新建了一個 book 文章類型,下面我們為這個 book 文章類型添加一個國家-country 分類法支持,完整代碼如下 (保留了上面添加的分類和標籤支持):

  1. <?php   
  2. add_action('init', 'my_custom_init');   
  3. function my_custom_init()    
  4. {   
  5.   $labels = array(   
  6.     'name' => '書本 name',   
  7.     'singular_name' => '書本 singularname',   
  8.     'add_new' => 'Add_new',   
  9.     'add_new_item' => 'add_new_item',   
  10.     'edit_item' => 'edit_item',   
  11.     'new_item' => 'new_item',   
  12.     'view_item' => 'view_item',   
  13.     'search_items' => 'search_items',   
  14.     'not_found' =>  'not_found',   
  15.     'not_found_in_trash' => 'not_found_in_trash',    
  16.     'parent_item_colon' => '',   
  17.     'menu_name' => 'menu_name'   
  18.   
  19.   );   
  20.   $args = array(   
  21.     'labels' => $labels,   
  22.     'description'=> '嘿,這是一個自定義的文章類型',   
  23.     'public' => true,
  24.     'publicly_queryable' => true,   
  25.     'show_ui' => true,    
  26.     'show_in_menu' => true,    
  27.     'query_var' => true,   
  28.     'rewrite' => true,   
  29.     'capability_type' => 'post',   
  30.     'has_archive' => true,    
  31.     'hierarchical' => false,   
  32.     'menu_position' => null,   
  33.     'taxonomies'=> array('category','post_tag'),   
  34.     'supports' => array('title','editor','author','thumbnail','excerpt','comments')   
  35.   );    
  36.   register_post_type('book',$args);   
  37.      
  38.   $labels = array(   
  39.         'name' => '國籍',    
  40.         'singular_name' => 'country',   
  41.         'search_items' =>  '搜索' ,   
  42.         'popular_items' => '熱門' ,   
  43.         'all_items' => '所有' ,   
  44.         'parent_item' => null,   
  45.         'parent_item_colon' => null,   
  46.         'edit_item' => '編輯' ,    
  47.         'update_item' => '更新' ,   
  48.         'add_new_item' => '添加' ,   
  49.         'new_item_name' => '國籍名稱',   
  50.         'separate_items_with_commas' => '按逗號分開' ,   
  51.         'add_or_remove_items' => '添加或刪除',   
  52.         'choose_from_most_used' => '從經常使用的類型中選擇',   
  53.         'menu_name' => '國籍分類',   
  54.     );    
  55.   
  56.     register_taxonomy(   
  57.         'country',   
  58.         array('book'),   
  59.         array(   
  60.             'hierarchical' => true,   
  61.             'labels' => $labels,   
  62.             'show_ui' => true,   
  63.             'query_var' => true,   
  64.             'rewrite' => array( 'slug' => 'country' ),   
  65.         )   
  66.     );   
  67. }   
  68. ?>  

 

更新--前台使用方法

文章類型歸檔模板:如果你需要一個現實所有該文章類型的模板,請在後台新建一個 archive-{post_type}.php, 比如上面的 book 類型,新建 archive-book.php,用這個模板文件默認可顯示所有 book 類型的文章。

分類模板:taxonomy-{taxonomy_slug}.php-這是自定義分類法的分類頁,比如上面代碼中我們新建了一個分類法 country,使用 taxonomy-country.php 文件,就是這個分類法的分類頁面了。

代碼獲取分類:對於默認的分類,我們可以使用 get_categories() 函數來獲取分類,對於自定義分類法,這個函數同樣適用,只是注意 taxonomy 參數。

通過 ID 獲取分類連接:默認分類我們通過分類 ID 獲取分類鏈接是使用函數:get_category_link() 。但是自定義分類法我們應該使用 get_term_link() 函數,函數用法這裏就不説了,請看官自己到官網查看。