WordPress注册自定义post类型代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WordPress注册自定义post类型代码相关的知识,希望对你有一定的参考价值。

  1. // Register Custom Post Type
  2. // snippet updated 6/3/2011
  3.  
  4. // Bare Bones
  5. // -- from Ian Stewart at bit.ly/wppoweredby
  6. function powered_by_create_post_type() {
  7. register_post_type( 'foo', array(
  8. 'public' => true,
  9. ) );
  10. }
  11. add_action( 'init', 'powered_by_create_post_type' );
  12.  
  13. // Basic and Quick
  14. // -- from Konstantin Kovshenin at bit.ly/kovcpt
  15. // -- also see Justin Tadlock at bit.ly/jtadcpt
  16. // -- and Custom Post Type Generator at bit.ly/generate-wpcpt
  17.  
  18. // Fire this during init
  19. register_post_type('foo', array(
  20. 'label' => __('Foos'),
  21. 'singular_label' => __('Foo'),
  22. 'public' => true,
  23. 'show_ui' => true,
  24. 'capability_type' => 'post',
  25. 'hierarchical' => false,
  26. 'rewrite' => false,
  27. 'query_var' => false,
  28. 'supports' => array('title', 'editor', 'author', 'excerpt', 'custom-fields')
  29. ));
  30.  
  31. // Full and Comprehensive
  32. // -- generated with the Custom Post Type Generator at bit.ly/generate-wpcpt
  33.  
  34. add_action( 'init', 'register_cpt_foo' );
  35. function register_cpt_foo() {
  36. $labels = array(
  37. 'name' => _x( 'Foos', 'foo' ),
  38. 'singular_name' => _x( 'foo', 'foo' ),
  39. 'add_new' => _x( 'Add New', 'foo' ),
  40. 'add_new_item' => _x( 'Add New foo', 'foo' ),
  41. 'edit_item' => _x( 'Edit foo', 'foo' ),
  42. 'new_item' => _x( 'New foo', 'foo' ),
  43. 'view_item' => _x( 'View foo', 'foo' ),
  44. 'search_items' => _x( 'Search Foos', 'foo' ),
  45. 'not_found' => _x( 'No foos found', 'foo' ),
  46. 'not_found_in_trash' => _x( 'No foos found in Trash', 'foo' ),
  47. 'parent_item_colon' => _x( 'Parent foo:', 'foo' ),
  48. 'menu_name' => _x( 'Foos', 'foo' ),
  49. );
  50. $args = array(
  51. 'labels' => $labels,
  52. 'hierarchical' => true,
  53. 'description' => 'These are foos.',
  54. 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'custom-fields' ),
  55. 'taxonomies' => array( 'category' ),
  56. 'public' => true,
  57. 'show_ui' => true,
  58. 'show_in_menu' => true,
  59. 'show_in_nav_menus' => true,
  60. 'publicly_queryable' => true,
  61. 'exclude_from_search' => false,
  62. 'has_archive' => true,
  63. 'query_var' => true,
  64. 'can_export' => true,
  65. 'rewrite' => true,
  66. 'capability_type' => 'post'
  67. );
  68. register_post_type( 'foo', $args );
  69. }
  70. // end

以上是关于WordPress注册自定义post类型代码的主要内容,如果未能解决你的问题,请参考以下文章

从 Wordpress 取消注册自定义帖子类型

Wordpress注册自定义Post类型和自定义分类法

Wordpress注册自定义post类型

没有自定义帖子类型 slug 的 WordPress slug

Wordpress 自定义帖子类型类别

用于自定义帖子类型的 Wordpress 多个 slug