內容營銷
具有自定義類別的自定義帖子類型
WordPress已成為許多公司不可或缺的平台,但是普通公司甚至沒有利用其中的一部分功能。 我們的一位客戶希望在其網站上添加資源部分,但不希望使用頁面或博客文章來添加。 這正是WordPress支持的 自定義文章類型 對於!
在這種情況下,我們希望將資源部分添加到客戶的站點之一。 添加一個很簡單 Custom Post Type 到您的WordPress主題。 您使用函數添加以下代碼 register_post_type 到您的functions.php頁面:
//添加資源自定義帖子類型add_action('init','create_post_type'); 函數create_post_type(){register_post_type('resources',array('labels'=> array('name'=> __('Resources'),'singular_name'=> __('Resource'),'add_new'=> __ ('Add New'),'add_new_item'=> __('Add New Resource'),'edit_item'=> __('Edit Resource'),'new_item'=> __('New Resource'),'all_items' => __('所有資源'),'view_item'=> __('View Resource'),'search_items'=> __('Search Resources'),'not_found'=> __('Resource Not Found'), 'not_found_in_trash'=> __('垃圾桶中沒有資源'),'parent_item_colon'=>'','menu_name'=> __('Resources')),'public'=> true,'has_archive'=> true, 'rewrite'=> array('slug'=>'resources'),'supports'=> array('title','editor','author','thumbnail','excerpt','comments')))) ; }
很難找到的是如何製作 自定義類別 關於 Custom Post Type。 之所以很難做到這一點,是因為它被稱為自定義分類法,並且利用了
註冊分類法 自定義它的功能。 在這種情況下,我們希望將網絡研討會、白皮書等資源類型添加到主題中……所以這裡有一些 functions.php 文件的附加代碼:add_action('init','resource_category_init',100); // 100,因此已註冊帖子類型function resource_category_init(){register_taxonomy('type','resources',array('labels'=> array('name'=>'Resource Type','singular_name'=>'資源類型'','search_items'=>'搜索資源類型','popular_items'=>'流行資源類型','all_items'=>'所有資源類型','edit_item'=> __('Edit Resource Type') ,'update_item'=> __('Update Resource Type'),'add_new_item'=> __('Add New Resource Type'),'new_item_name'=> __('New Resource Type')),'hierarchical'=> 'false','label'=>'資源類型')); }
自定義帖子類型還允許您設計自定義帖子類型的檔案和單個頁面。 只需複制archive.php和single.php文件。 將副本重命名為 Custom Post Type 在名字裡。 在這種情況下,它將是archive-resources.php和single-resources.php。 現在,您可以自定義這些頁面,但是希望資源頁面看起來像。