内容营销
具有自定义类别的自定义帖子类型
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。 现在,您可以自定义这些页面,但是希望资源页面显示出来。