wordpress同时创建多个自定义文章类型的方法

WordPress教程 1688

wordpress创建自定义文章类型需要设置很多个参数,如果仅仅是创建一个自定义文章类型对代码的简洁性不会有什么影响,但是如果要创建多个自定义文章类型的时候,就要添加重复的参数代码,不仅麻烦而且不易管理。为了代码的简洁性和易于管理,可以把注册自定义文章类型的代码写成函数,需要创建时直接调用函数即可。

函数代码:
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
// register post type label args 
function cutom_post_type_label_args($name, $slugName){
    return $labels = array(
        'name' => $name,
        'singular_name' => $slugName,
        'add_new' => '新增',
        'add_new_item' => '新增 '.$name,
        'edit_item' => '编辑',
        'new_item' => '新加',
        'all_items' => '所有'.$name,
        'view_item' => '浏览'.$name,
        'search_items' => '搜索'.$name,
        'not_found' =>  '没有发现'.$name,
        'not_found_in_trash' => '回收站没有'.$name, 
        'parent_item_colon' => '',
        'menu_name' => $name
    );
}
 
// register post type args 
function custom_post_type_args($name, $slugName, $postType='post', $public=true, $queryable=true, $show_ui=true, $show_menu=true, $query_var=true, $has_archive = true, $hierarchical = false, $menu_position = null, $supports = array(), $menu_icon=null){
    return $args = array(
        'labels' => cutom_post_type_label_args($name,$slugName),
        'public' => $public,
        'publicly_queryable' => $queryable,
        'show_ui' => $show_ui, 
        'show_in_menu' => $show_menu, 
        'query_var' => $query_var,
        'rewrite' => array( 'slug' => strtolower($slugName)),
        'capability_type' => $postType,
        'has_archive' => $has_archive, 
        'hierarchical' => $hierarchical,
        'menu_position' => $menu_position,
        'supports' => $supports,
        'menu_icon' => $menu_icon
    );
}
然后通过以下代码快速创建自定义文章类型:
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
function add_custom_post_type() {
    register_post_type( 'slides_type', 
        custom_post_type_args(
            '产品',//$name
            'products',//$slugName
            'post',//$postType
            false,//$public
            false,//$queryable
            true,//$show_ui
            true,//$show_menu
            true,//$query_var
            false,//$has_archive
            false,//$hierarchical
            8,//$menu_position
            array('title'),//$supports
            'dashicons-images-alt2' //$menu_icon
        )
    );
    register_post_type( 'piclink_type', 
        custom_post_type_args(
            '视频',//$name
            'video',//$slugName
            'post',//$postType
            false,//$public
            false,//$queryable
            true,//$show_ui
            true,//$show_menu
            true,//$query_var
            false,//$has_archive
            false,//$hierarchical
            8,//$menu_position
            array('title'),//$supports
            'dashicons-images-alt2' //$menu_icon
        )
    );
}
add_action( 'init', 'add_custom_post_type' );

注:每一个register_post_type表示创建一个自定义文章类型,注意修改类型名称。

参考网站:wzbs.org

精品推荐: