WordPress添加自定义类型文章、分类和标签

今天无意中,看到了wordpress的一篇有用文章,赶紧推荐给大家。

网上大部分人分享添加 WordPress 自定义类型文章的代码,一般都是包含了文章和分类,最多就是添加一个跟文章类型 post 共用一个标签库,但是默认标签库的数量是增加了,但是点击查看该标签文章却没有那么多,只能看到 post 类型的文章,而看不到自定义文章类型,所以最好的方案就是为自定义类型的文章弄一个独立的标签库。

WordPress添加自定义类型文章、分类和标签

将以下代码添加到当前主题的 functions.php 文件中,或者添加到一个文章类型的 post_type.php 文件,然后在 functions.php 文件中引用该文章类型文件即可。

  1. // 商品

  2. add_action( 'init', 'post_type_tao' );

  3. function post_type_tao() {

  4. $labels = array(

  5. 'name' => '商品', 'post type general name', 'your-plugin-textdomain',

  6. 'singular_name' => '商品', 'post type singular name', 'your-plugin-textdomain',

  7. 'menu_name' => '商品', 'admin menu', 'your-plugin-textdomain',

  8. 'name_admin_bar' => '商品', 'add new on admin bar', 'your-plugin-textdomain',

  9. 'add_new' => '发布商品', 'tao', 'your-plugin-textdomain',

  10. 'add_new_item' => '发布新商品', 'your-plugin-textdomain',

  11. 'new_item' => '新商品', 'your-plugin-textdomain',

  12. 'edit_item' => '编辑商品', 'your-plugin-textdomain',

  13. 'view_item' => '查看商品', 'your-plugin-textdomain',

  14. 'all_items' => '所有商品', 'your-plugin-textdomain',

  15. 'search_items' => '搜索商品', 'your-plugin-textdomain',

  16. 'parent_item_colon' => 'Parent 商品:', 'your-plugin-textdomain',

  17. 'not_found' => '你还没有发布商品。', 'your-plugin-textdomain',

  18. 'not_found_in_trash' => '回收站中没有商品。', 'your-plugin-textdomain'

  19. );

  20. $args = array(

  21. 'labels' => $labels,

  22. 'public' => true,

  23. 'publicly_queryable' => true,

  24. 'show_ui' => true,

  25. 'show_in_menu' => true,

  26. 'query_var' => true,

  27. 'rewrite' => array( 'slug' => tao ),

  28. 'capability_type' => 'post',

  29. 'menu_icon' => 'dashicons-cart',

  30. 'has_archive' => false,

  31. 'hierarchical' => false,

  32. 'menu_position' => 10,

  33. 'supports' => array( 'title', 'editor', 'author', 'excerpt', 'comments', 'thumbnail', 'revisions', 'custom-fields' )

  34. );

  35. register_post_type( 'tao', $args );

  36. }

  37. // 商品分类

  38. add_action( 'init', 'create_tao_taxonomies', 0 );

  39. function create_tao_taxonomies() {

  40. $labels = array(

  41. 'name' => '商品分类目录', 'taxonomy general name',

  42. 'singular_name' => '商品分类', 'taxonomy singular name',

  43. 'search_items' => '搜索商品目录',

  44. 'all_items' => '所有商品目录',

  45. 'parent_item' => 'Parent Genre',

  46. 'parent_item_colon' => 'Parent Genre:',

  47. 'edit_item' => '编辑商品目录',

  48. 'update_item' => '更新商品目录',

  49. 'add_new_item' => '添加新商品目录',

  50. 'new_item_name' => 'New Genre Name',

  51. 'menu_name' => '商品分类',

  52. );

  53. $args = array(

  54. 'hierarchical' => true,

  55. 'labels' => $labels,

  56. 'show_ui' => true,

  57. 'show_admin_column' => true,

  58. 'query_var' => true,

  59. 'rewrite' => array( 'slug' => taobao),

  60. );

  61. register_taxonomy( 'taobao', array( 'tao' ), $args );

  62. }

  63. // 商品标签

  64. add_action( 'init', 'create_tao_tag_taxonomies', 0 );

  65. function create_tao_tag_taxonomies() {

  66. $labels = array(

  67. 'name' => '商品标签', 'taxonomy general name',

  68. 'singular_name' => '商品标签', 'taxonomy singular name',

  69. 'search_items' => '搜索商品标签',

  70. 'all_items' => '所有商品标签',

  71. 'parent_item' => 'Parent Genre',

  72. 'parent_item_colon' => 'Parent Genre:',

  73. 'edit_item' => '编辑商品标签',

  74. 'update_item' => '更新商品标签',

  75. 'add_new_item' => '添加新商品标签',

  76. 'new_item_name' => 'New Genre Name',

  77. 'menu_name' => '商品标签',

  78. );

  79. $args = array(

  80. 'hierarchical' => false,

  81. 'labels' => $labels,

  82. 'show_ui' => true,

  83. 'show_admin_column' => true,

  84. 'query_var' => true,

  85. 'rewrite' => array( 'slug' => taotag ),

  86. );

  87. register_taxonomy( 'taotag', array( 'tao' ), $args );

  88. }

以上代码实现的功能就是为 WordPress 站点添加自定义类型的文章、分类和标签,具体请自行修改里面自定义类型的标题、slug 等内容。想创建多个自定义类型文章、分类和标签,直接重复上述代码,并修改为其他的标题及 slug 即可。

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: