当前位置:首页>WordPress建站>WordPress开发>WordPress快速添加多个自定义文章类型

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

在添加WordPress自定义文章类型(custom post type)的时候,因为参数很多,会有很长的参数数组。如官方文档中的例子:

  1. function codex_custom_init() {
  2. $labels = array(
  3. 'name' => 'Books',
  4. 'singular_name' => 'Book',
  5. 'add_new' => 'Add New',
  6. 'add_new_item' => 'Add New Book',
  7. 'edit_item' => 'Edit Book',
  8. 'new_item' => 'New Book',
  9. 'all_items' => 'All Books',
  10. 'view_item' => 'View Book',
  11. 'search_items' => 'Search Books',
  12. 'not_found' => 'No books found',
  13. 'not_found_in_trash' => 'No books found in Trash',
  14. 'parent_item_colon' => '',
  15. 'menu_name' => 'Books'
  16. );
  17. $args = array(
  18. 'labels' => $labels,
  19. 'public' => true,
  20. 'publicly_queryable' => true,
  21. 'show_ui' => true,
  22. 'show_in_menu' => true,
  23. 'query_var' => true,
  24. 'rewrite' => array( 'slug' => 'book' ),
  25. 'capability_type' => 'post',
  26. 'has_archive' => true,
  27. 'hierarchical' => false,
  28. 'menu_position' => null,
  29. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  30. );
  31. register_post_type( 'book', $args );
  32. }
  33. add_action( 'init', 'codex_custom_init' );

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

  1. // register post type label args
  2. function tj_cutom_post_type_label_args($typeName){
  3. return $labels = array(
  4. 'name' => $typeName,
  5. 'singular_name' => $typeName,
  6. 'add_new' => 'Add New',
  7. 'add_new_item' => 'Add New '.$typeName,
  8. 'edit_item' => 'Edit '.$typeName,
  9. 'new_item' => 'New '.$typeName,
  10. 'all_items' => 'All '.$typeName,
  11. 'view_item' => 'View '.$typeName,
  12. 'search_items' => 'Search '.$typeName,
  13. 'not_found' => 'No '.$typeName.' found',
  14. 'not_found_in_trash' => 'No '.$typeName.' found in Trash',
  15. 'parent_item_colon' => '',
  16. 'menu_name' => $typeName
  17. );
  18. }
  19. // register post type args
  20. 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){
  21. return $args = array(
  22. 'labels' => tj_cutom_post_type_label_args($typeName),
  23. 'public' => $public,
  24. 'publicly_queryable' => $queryable,
  25. 'show_ui' => $show_ui,
  26. 'show_in_menu' => $show_menu,
  27. 'query_var' => $query_var,
  28. 'rewrite' => array( 'slug' => strtolower($typeName)),
  29. 'capability_type' => $postType,
  30. 'has_archive' => $has_archive,
  31. 'hierarchical' => $hierarchical,
  32. 'menu_position' => $menu_position,
  33. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  34. );
  35. }

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

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

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
欢迎关注WordPress大学公众号 WPDAXUE
WordPress开发

WordPress函数:remove meta box(移除Meta模块)

2013-6-9 11:12:10

WordPress开发

如何获取WordPress当前用户信息

2013-6-12 7:36:32

7 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
  1. 上面代码第4,5行typeName前面少了$

  2. 用这个方法怎么添加多个自定义文章类型的分类呢???

    • ❓ 同样的问题。。。。站长能说明下吗

    • 对呀,没有分类和tag标签

  3. defive

    自定义文章类型的文章 管理里面,怎么添加 按分类目录筛选的功能呢?

  4. 这个方法方便点 但目前只增加过一个 还不需要

  5. 哎呀,我咋没早点发现这篇文章,白瞎了我这么些日子~~

个人中心
购物车
优惠劵
今日签到
私信列表
搜索