前面剛建了給 WordPress 建立新的文章型別函式:register_post_type() 該函式還有個 taxonomies 引數,用來給自定義文章型別新增分類法制止,給 WordPress 新增預設的分類和標籤支援方法很簡單, 只需要在建立文章型別的時候設定 taxonomies 引數如下:
- 'taxonomies'=> array('category','post_tag'),
效果如圖:

然後這在實際中的用法很少,一般情況下我們使用新的分類法,比如我們要做一個企業站,給產品做了一個新的文章型別 product 。我們要給產品歸類,可以按類別分,比如電腦、手機。還可以按產地分,比如:國產山寨、水貨,還可以按價格區間分,比如:1000 元以下、 1000 到 3000,等等,我們就需要同時有 3 個分類法:類別、產地、價格區間。
在本部分教程的第一篇中我們也提到了,在 wp-includes/post.php 檔案中 WordPress 使用 register_taxonomy 函式建立了分類-category 和標籤-post_tag 兩個分類法。
下面是 register_taxonomy 函式的引數好用法:
- <?php
- register_taxonomy($taxonomy, $object_type, $args);
- //$taxonomy 字串型,必需,分類法的名稱,用英文哦
- //$object_type 陣列或字串,必需,分類法所對應的文章型別
- //$args--可選,配置引數
- ?>
$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 分類法支援,完整程式碼如下 (保留了上面新增的分類和標籤支援):
- <?php
- add_action('init', 'my_custom_init');
- function my_custom_init()
- {
- $labels = array(
- 'name' => '書本 name',
- 'singular_name' => '書本 singularname',
- 'add_new' => 'Add_new',
- 'add_new_item' => 'add_new_item',
- 'edit_item' => 'edit_item',
- 'new_item' => 'new_item',
- 'view_item' => 'view_item',
- 'search_items' => 'search_items',
- 'not_found' => 'not_found',
- 'not_found_in_trash' => 'not_found_in_trash',
- 'parent_item_colon' => '',
- 'menu_name' => 'menu_name'
- );
- $args = array(
- 'labels' => $labels,
- 'description'=> '嘿,這是一個自定義的文章型別',
- 'public' => true,
- 'publicly_queryable' => true,
- 'show_ui' => true,
- 'show_in_menu' => true,
- 'query_var' => true,
- 'rewrite' => true,
- 'capability_type' => 'post',
- 'has_archive' => true,
- 'hierarchical' => false,
- 'menu_position' => null,
- 'taxonomies'=> array('category','post_tag'),
- 'supports' => array('title','editor','author','thumbnail','excerpt','comments')
- );
- register_post_type('book',$args);
- $labels = array(
- 'name' => '國籍',
- 'singular_name' => 'country',
- 'search_items' => '搜尋' ,
- 'popular_items' => '熱門' ,
- 'all_items' => '所有' ,
- 'parent_item' => null,
- 'parent_item_colon' => null,
- 'edit_item' => '編輯' ,
- 'update_item' => '更新' ,
- 'add_new_item' => '新增' ,
- 'new_item_name' => '國籍名稱',
- 'separate_items_with_commas' => '按逗號分開' ,
- 'add_or_remove_items' => '新增或刪除',
- 'choose_from_most_used' => '從經常使用的型別中選擇',
- 'menu_name' => '國籍分類',
- );
- register_taxonomy(
- 'country',
- array('book'),
- array(
- 'hierarchical' => true,
- 'labels' => $labels,
- 'show_ui' => true,
- 'query_var' => true,
- 'rewrite' => array( 'slug' => 'country' ),
- )
- );
- }
- ?>

更新--前臺使用方法:
文章型別歸檔模板:如果你需要一個現實所有該文章型別的模板,請在後臺新建一個 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() 函式,函式用法這裡就不說了,請看官自己到官網檢視。