如何創建 WordPress 自定義帖子類型 (CPT) 和分類法 – Hello World 教程、提示和技巧
已發表: 2016-03-11 驚人的。 讓我們談談Custom Post Type
(CPT)。 如果有以下問題,這也將對您有所幫助:
- 如何在 WordPress 中創建自定義帖子類型?
- 生成 WordPress 帖子類型
- 創建您的第一個 WordPress 自定義帖子類型
- WordPress CPT 分類
- WordPress自定義帖子類型的完整指南
WordPress 是使用最廣泛的博客平台之一,具有許多強大的功能, Custom Post Type
就是其中之一。 我以前從未在 WordPress 中查看過自定義帖子類型 (CPT),但最近我想為“交易”創建一個新部分。
有兩種方法可以實現:
- 創建新子域:
http://deals.crunchify.com/
://deals.crunchify.com/ - 創建新的自定義帖子類型:
https://crunchify.com/deals/
://crunchify.com/deals/
要實現point 1
,您可以簡單地創建一個子域並安裝新的 WordPress 安裝並啟動新博客。 但我相信,沒有人願意這樣做來創建一個簡單的部分。 因此,我們將看看本教程中的point 2
。
WordPress有一堆默認的帖子類型,比如:
- 附件(帖子類型:“附件”)
- 帖子(帖子類型:'post')
- 導航菜單(帖子類型:'nav_menu_item')
- 修訂(帖子類型:'修訂')
- 頁面(帖子類型:“頁面”)
現在什麼是CPT?
Custom post types (CPT)
是您可以創建的新帖子類型。 可以通過register_post_type()函數將自定義帖子類型添加到 WordPress。 此功能允許您通過標籤、支持的功能、可用性和其他細節來定義新的帖子類型。
讓我們開始吧。
第1步
這是我們需要創建一個名為“ Deals
”的新Custom Post Type
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
// Creating a Deals Custom Post Type function crunchify_deals_custom_post_type ( ) { $labels = array ( 'name' = > __ ( 'Deals' ) , 'singular_name' = > __ ( 'Deal' ) , 'menu_name' = > __ ( 'Deals' ) , 'parent_item_colon' = > __ ( 'Parent Deal' ) , 'all_items' = > __ ( 'All Deals' ) , 'view_item' = > __ ( 'View Deal' ) , 'add_new_item' = > __ ( 'Add New Deal' ) , 'add_new' = > __ ( 'Add New' ) , 'edit_item' = > __ ( 'Edit Deal' ) , 'update_item' = > __ ( 'Update Deal' ) , 'search_items' = > __ ( 'Search Deal' ) , 'not_found' = > __ ( 'Not Found' ) , 'not_found_in_trash' = > __ ( 'Not found in Trash' ) ) ; $args = array ( 'label' = > __ ( 'deals' ) , 'description' = > __ ( 'Best Crunchify Deals' ) , 'labels' = > $labels , 'supports' = > array ( 'title' , 'editor' , 'excerpt' , 'author' , 'thumbnail' , 'revisions' , 'custom-fields' ) , 'public' = > true , 'hierarchical' = > false , 'show_ui' = > true , 'show_in_menu' = > true , 'show_in_nav_menus' = > true , 'show_in_admin_bar' = > true , 'has_archive' = > true , 'can_export' = > true , 'exclude_from_search' = > false , 'yarpp_support' = > true , 'taxonomies' = > array ( 'post_tag' ) , 'publicly_queryable' = > true , 'capability_type' = > 'page' ) ; register_post_type ( 'deals' , $args ) ; } add_action ( 'init' , 'crunchify_deals_custom_post_type' , 0 ) ; |
上面的代碼將創建一個名為Deals
的新部分,我們稱之為 Custom Post Type

如何將標籤添加到自定義帖子類型? 上面代碼段中
'taxonomies' => array('post_tag')
行會將標準 WordPress 標籤添加到您的自定義帖子類型中。
第2步
現在,如果您想創建名為Type
的Custom Taxonomy
。 使用register_taxonomy
函數,您可以簡單地創建分類。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// Let us create Taxonomy for Custom Post Type add_action ( 'init' , 'crunchify_create_deals_custom_taxonomy' , 0 ) ; //create a custom taxonomy name it "type" for your posts function crunchify_create_deals_custom_taxonomy ( ) { $labels = array ( 'name' = > _x ( 'Types' , 'taxonomy general name' ) , 'singular_name' = > _x ( 'Type' , 'taxonomy singular name' ) , 'search_items' = > __ ( 'Search Types' ) , 'all_items' = > __ ( 'All Types' ) , 'parent_item' = > __ ( 'Parent Type' ) , 'parent_item_colon' = > __ ( 'Parent Type:' ) , 'edit_item' = > __ ( 'Edit Type' ) , 'update_item' = > __ ( 'Update Type' ) , 'add_new_item' = > __ ( 'Add New Type' ) , 'new_item_name' = > __ ( 'New Type Name' ) , 'menu_name' = > __ ( 'Types' ) , ) ; register_taxonomy ( 'types' , array ( 'deals' ) , array ( 'hierarchical' = > true , 'labels' = > $labels , 'show_ui' = > true , 'show_admin_column' = > true , 'query_var' = > true , 'rewrite' = > array ( 'slug' = > 'type' ) , ) ) ; } |
第三步
現在創建您的第一個交易帖子。 這是Add New Deal
的屏幕截圖。
第四步
創建您的第一個類型分類。 這是一個Add New Type
頁面。
我們使用本教程創建了 WordPress Custom Post Type - Deals
和Custom Taxonomy - Types
。 在下一個教程中,我們將對此進行更多的自定義。
接下來是什麼? 獲取 RSS、YARPP、帖子元、頁腳、主頁的 WordPress 自定義帖子類型 (CPT) 提示和技巧。
另外,查看另一個關於 CPT 的教程。
如何在自定義帖子類型永久鏈接中添加自定義分類?