前面剛建了給 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() 函數,函數用法這裏就不説了,請看官自己到官網查看。