wordpress自定义文章类型功能添加?
我们在做wordpress企业站的时候常常需要增加自定义文章类型
类似于产品中心,案例中心,新闻资讯
不同分类需要调用不同的模板,所以我们可以通过自定义文章类型来定义
下面以添加电影资源来举例:
1.在主题函数functions.php中添加一下代码
function create_movie_post_type() {
$labels = array(
'name' => _x( '电影列表', 'textdomain' ),
'singular_name' => _x( '电影列表', 'textdomain' ),
'menu_name' => _x( '电影列表', 'textdomain' ),
'name_admin_bar' => _x( '电影列表', 'textdomain' ),
'add_new' => __( '添加新电影', 'textdomain' ),
'add_new_item' => __( '添加新电影', 'textdomain' ),
'new_item' => __( '添加新电影', 'textdomain' ),
'edit_item' => __( '编辑电影', 'textdomain' ),
'view_item' => __( '查看电影', 'textdomain' ),
'all_items' => __( '所有电影', 'textdomain' ),
'search_items' => __( '搜索电影', 'textdomain' ),
'parent_item_colon' => __( '父电影:', 'textdomain' ),
'not_found' => __( '未发现任何电影.', 'textdomain' ),
'not_found_in_trash' => __( '垃圾箱没有任何电影.', 'textdomain' ),
'featured_image' => _x( '电影封面', 'textdomain' ),
'set_featured_image' => _x( '设置电影封面', 'textdomain' ),
'remove_featured_image' => _x( '移除电影封面', 'textdomain' ),
'use_featured_image' => _x( '设置为封面', 'textdomain' ),
'archives' => _x( '电影归档', 'textdomain' ),
'insert_into_item' => _x( '插入到电影', 'textdomain' ),
'uploaded_to_this_item' => _x( '上传该电影', 'textdomain' ),
'filter_items_list' => _x( '筛选电影', 'textdomain' ),
'items_list_navigation' => _x( '电影导航', 'textdomain' ),
'items_list' => _x( '电影列表', 'textdomain' ),
);
$args = array(
'menu_icon' => 'dashicons-format-video',
'labels' => $labels,
//是否公开
'public' => true,
//是否可查询
'publicly_queryable' => true,
//显示在后台菜单中
'show_ui' => true,
//显示在后台菜单中
'show_in_menu' => true,
//是否可以查询,和publicly_queryable一起使用
'query_var' => true,
//重写url
'rewrite' => array( 'slug' => 'movie' ),
//该文章类型的权限
'capability_type' => 'post',
//是否有归档
'has_archive' => true,
//是否水平,如果水平就是页面,否则类似文章这种可以有分类目录(需要自定义分类目录)
'hierarchical' => false,
//菜单定位
'menu_position' => 2,
//该文章类型支持的功能
'supports' => array( 'title', 'author', 'thumbnail', 'excerpt', 'comments' ),
);
register_post_type( 'movie', $args );
}
add_action( 'init', 'create_movie_post_type' );