WordPress快速添加多个自定义文章类型

秋天是收获的季节。柿子树上缀满了小红灯笼似的柿子,沉甸甸的,把枝头都压弯了。枫树的叶子火红火红的,像一堆正在燃烧的火焰。那梧桐树的枯叶在秋风中纷纷飘落下来,像翩翩起舞的金色蝴蝶。

在添加WordPress自定义文章类型(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
function codex_custom_init() {
  $labels = array(
    'name' => 'Books',
    'singular_name' => 'Book',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Book',
    'edit_item' => 'Edit Book',
    'new_item' => 'New Book',
    'all_items' => 'All Books',
    'view_item' => 'View Book',
    'search_items' => 'Search Books',
    'not_found' =>  'No books found',
    'not_found_in_trash' => 'No books found in Trash', 
    'parent_item_colon' => '',
    'menu_name' => 'Books'
  );

  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => array( 'slug' => 'book' ),
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  ); 

  register_post_type( 'book', $args );
}
add_action( 'init', 'codex_custom_init' );

function codex_custom_init() { $labels = array( 'name' => 'Books', 'singular_name' => 'Book', 'add_new' => 'Add New', 'add_new_item' => 'Add New Book', 'edit_item' => 'Edit Book', 'new_item' => 'New Book', 'all_items' => 'All Books', 'view_item' => 'View Book', 'search_items' => 'Search Books', 'not_found' => 'No books found', 'not_found_in_trash' => 'No books found in Trash', 'parent_item_colon' => '', 'menu_name' => 'Books' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'book' ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) ); register_post_type( 'book', $args ); } add_action( 'init', 'codex_custom_init' );

这样写起来并不难,但是试想一下,如果我们的项目有要注册多个自定义文章类型的时候,那代码不是变得更长?所以就考虑将它们写成函数,分别定义好了这些参数数组,这样无论注册多少个自定义文章类型,代码都非常简单。

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
// register post type label args 
function tj_cutom_post_type_label_args($typeName){
    return $labels = array(
        'name' => $typeName,
        'singular_name' => $typeName,
        'add_new' => 'Add New',
        'add_new_item' => 'Add New '.$typeName,
        'edit_item' => 'Edit '.$typeName,
        'new_item' => 'New '.$typeName,
        'all_items' => 'All '.$typeName,
        'view_item' => 'View '.$typeName,
        'search_items' => 'Search '.$typeName,
        'not_found' =>  'No '.$typeName.' found',
        'not_found_in_trash' => 'No '.$typeName.' found in Trash', 
        'parent_item_colon' => '',
        'menu_name' => $typeName
    );
}

// register post type args 
function tj_custom_post_type_args($typeName,$postType="post",$public=true,$queryable=true,$show_ui=true,$show_menu=true,$query_var=true,$has_archive = true, $hierarchical = false,$menu_position = null){
    return $args = array(
        'labels' => tj_cutom_post_type_label_args($typeName),
        'public' => $public,
        'publicly_queryable' => $queryable,
        'show_ui' => $show_ui, 
        'show_in_menu' => $show_menu, 
        'query_var' => $query_var,
        'rewrite' => array( 'slug' => strtolower($typeName)),
        'capability_type' => $postType,
        'has_archive' => $has_archive, 
        'hierarchical' => $hierarchical,
        'menu_position' => $menu_position,
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );
}

// register post type label args function tj_cutom_post_type_label_args($typeName){ return $labels = array( 'name' => $typeName, 'singular_name' => $typeName, 'add_new' => 'Add New', 'add_new_item' => 'Add New '.$typeName, 'edit_item' => 'Edit '.$typeName, 'new_item' => 'New '.$typeName, 'all_items' => 'All '.$typeName, 'view_item' => 'View '.$typeName, 'search_items' => 'Search '.$typeName, 'not_found' => 'No '.$typeName.' found', 'not_found_in_trash' => 'No '.$typeName.' found in Trash', 'parent_item_colon' => '', 'menu_name' => $typeName ); } // register post type args function tj_custom_post_type_args($typeName,$postType="post",$public=true,$queryable=true,$show_ui=true,$show_menu=true,$query_var=true,$has_archive = true, $hierarchical = false,$menu_position = null){ return $args = array( 'labels' => tj_cutom_post_type_label_args($typeName), 'public' => $public, 'publicly_queryable' => $queryable, 'show_ui' => $show_ui, 'show_in_menu' => $show_menu, 'query_var' => $query_var, 'rewrite' => array( 'slug' => strtolower($typeName)), 'capability_type' => $postType, 'has_archive' => $has_archive, 'hierarchical' => $hierarchical, 'menu_position' => $menu_position, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) ); }

然后我们就可以通过下面的样例快速添加多个自定义文章类型啦:

1
2
3
4
5
function tj_custom_post_type() {
    register_post_type( 'portfolio', tj_custom_post_type_args("Portfolio"));
    register_post_type( 'testimonials', tj_custom_post_type_args("Testimonials"));
}
add_action( 'init', 'tj_custom_post_type' );

function tj_custom_post_type() { register_post_type( 'portfolio', tj_custom_post_type_args("Portfolio")); register_post_type( 'testimonials', tj_custom_post_type_args("Testimonials")); } add_action( 'init', 'tj_custom_post_type' );

参考资料:http://wzbs.org/wordpress-custom-content-types-efficient-wording/

到此这篇关于WordPress快速添加多个自定义文章类型就介绍到这了。穿过层层叠叠的流离时光,越过跌跌荡荡的万水千山,我一直走,一直走,来到你的面前,自此,驻足,沦落。早安!更多相关WordPress快速添加多个自定义文章类型内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
WordPress站点Gravatar头像前后台不显示的如何解决办法

WordPress做公司官网好吗?会不会显得档次很低?

WordPress主题需要支持https吗?WordPress站点如何如何实现https?

WordPress站点的页面/标签/分类URL地址如何添加.html?

WordPress站点更换了域名后数据库应该如何操作替换新旧域名?