custom-post-type.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /* Bones Custom Post Type Example
  3. This page walks you through creating
  4. a custom post type and taxonomies. You
  5. can edit this one or copy the following code
  6. to create another one.
  7. I put this in a separate file so as to
  8. keep it organized. I find it easier to edit
  9. and change things if they are concentrated
  10. in their own file.
  11. Developed by: Eddie Machado
  12. URL: http://themble.com/bones/
  13. */
  14. // Flush rewrite rules for custom post types
  15. add_action( 'after_switch_theme', 'bones_flush_rewrite_rules' );
  16. // Flush your rewrite rules
  17. function bones_flush_rewrite_rules() {
  18. flush_rewrite_rules();
  19. }
  20. // let's create the function for the custom type
  21. function custom_post_example() {
  22. // creating (registering) the custom type
  23. register_post_type( 'custom_type', /* (http://codex.wordpress.org/Function_Reference/register_post_type) */
  24. // let's now add all the options for this post type
  25. array( 'labels' => array(
  26. 'name' => __( 'Custom Types', 'bonestheme' ), /* This is the Title of the Group */
  27. 'singular_name' => __( 'Custom Post', 'bonestheme' ), /* This is the individual type */
  28. 'all_items' => __( 'All Custom Posts', 'bonestheme' ), /* the all items menu item */
  29. 'add_new' => __( 'Add New', 'bonestheme' ), /* The add new menu item */
  30. 'add_new_item' => __( 'Add New Custom Type', 'bonestheme' ), /* Add New Display Title */
  31. 'edit' => __( 'Edit', 'bonestheme' ), /* Edit Dialog */
  32. 'edit_item' => __( 'Edit Post Types', 'bonestheme' ), /* Edit Display Title */
  33. 'new_item' => __( 'New Post Type', 'bonestheme' ), /* New Display Title */
  34. 'view_item' => __( 'View Post Type', 'bonestheme' ), /* View Display Title */
  35. 'search_items' => __( 'Search Post Type', 'bonestheme' ), /* Search Custom Type Title */
  36. 'not_found' => __( 'Nothing found in the Database.', 'bonestheme' ), /* This displays if there are no entries yet */
  37. 'not_found_in_trash' => __( 'Nothing found in Trash', 'bonestheme' ), /* This displays if there is nothing in the trash */
  38. 'parent_item_colon' => ''
  39. ), /* end of arrays */
  40. 'description' => __( 'This is the example custom post type', 'bonestheme' ), /* Custom Type Description */
  41. 'public' => true,
  42. 'publicly_queryable' => true,
  43. 'exclude_from_search' => false,
  44. 'show_ui' => true,
  45. 'query_var' => true,
  46. 'menu_position' => 8, /* this is what order you want it to appear in on the left hand side menu */
  47. 'menu_icon' => get_stylesheet_directory_uri() . '/library/images/custom-post-icon.png', /* the icon for the custom post type menu */
  48. 'rewrite' => array( 'slug' => 'custom_type', 'with_front' => false ), /* you can specify its url slug */
  49. 'has_archive' => 'custom_type', /* you can rename the slug here */
  50. 'capability_type' => 'post',
  51. 'hierarchical' => false,
  52. /* the next one is important, it tells what's enabled in the post editor */
  53. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'sticky')
  54. ) /* end of options */
  55. ); /* end of register post type */
  56. /* this adds your post categories to your custom post type */
  57. register_taxonomy_for_object_type( 'category', 'custom_type' );
  58. /* this adds your post tags to your custom post type */
  59. register_taxonomy_for_object_type( 'post_tag', 'custom_type' );
  60. }
  61. // adding the function to the Wordpress init
  62. add_action( 'init', 'custom_post_example');
  63. /*
  64. for more information on taxonomies, go here:
  65. http://codex.wordpress.org/Function_Reference/register_taxonomy
  66. */
  67. // now let's add custom categories (these act like categories)
  68. register_taxonomy( 'custom_cat',
  69. array('custom_type'), /* if you change the name of register_post_type( 'custom_type', then you have to change this */
  70. array('hierarchical' => true, /* if this is true, it acts like categories */
  71. 'labels' => array(
  72. 'name' => __( 'Custom Categories', 'bonestheme' ), /* name of the custom taxonomy */
  73. 'singular_name' => __( 'Custom Category', 'bonestheme' ), /* single taxonomy name */
  74. 'search_items' => __( 'Search Custom Categories', 'bonestheme' ), /* search title for taxomony */
  75. 'all_items' => __( 'All Custom Categories', 'bonestheme' ), /* all title for taxonomies */
  76. 'parent_item' => __( 'Parent Custom Category', 'bonestheme' ), /* parent title for taxonomy */
  77. 'parent_item_colon' => __( 'Parent Custom Category:', 'bonestheme' ), /* parent taxonomy title */
  78. 'edit_item' => __( 'Edit Custom Category', 'bonestheme' ), /* edit custom taxonomy title */
  79. 'update_item' => __( 'Update Custom Category', 'bonestheme' ), /* update title for taxonomy */
  80. 'add_new_item' => __( 'Add New Custom Category', 'bonestheme' ), /* add new title for taxonomy */
  81. 'new_item_name' => __( 'New Custom Category Name', 'bonestheme' ) /* name title for taxonomy */
  82. ),
  83. 'show_admin_column' => true,
  84. 'show_ui' => true,
  85. 'query_var' => true,
  86. 'rewrite' => array( 'slug' => 'custom-slug' ),
  87. )
  88. );
  89. // now let's add custom tags (these act like categories)
  90. register_taxonomy( 'custom_tag',
  91. array('custom_type'), /* if you change the name of register_post_type( 'custom_type', then you have to change this */
  92. array('hierarchical' => false, /* if this is false, it acts like tags */
  93. 'labels' => array(
  94. 'name' => __( 'Custom Tags', 'bonestheme' ), /* name of the custom taxonomy */
  95. 'singular_name' => __( 'Custom Tag', 'bonestheme' ), /* single taxonomy name */
  96. 'search_items' => __( 'Search Custom Tags', 'bonestheme' ), /* search title for taxomony */
  97. 'all_items' => __( 'All Custom Tags', 'bonestheme' ), /* all title for taxonomies */
  98. 'parent_item' => __( 'Parent Custom Tag', 'bonestheme' ), /* parent title for taxonomy */
  99. 'parent_item_colon' => __( 'Parent Custom Tag:', 'bonestheme' ), /* parent taxonomy title */
  100. 'edit_item' => __( 'Edit Custom Tag', 'bonestheme' ), /* edit custom taxonomy title */
  101. 'update_item' => __( 'Update Custom Tag', 'bonestheme' ), /* update title for taxonomy */
  102. 'add_new_item' => __( 'Add New Custom Tag', 'bonestheme' ), /* add new title for taxonomy */
  103. 'new_item_name' => __( 'New Custom Tag Name', 'bonestheme' ) /* name title for taxonomy */
  104. ),
  105. 'show_admin_column' => true,
  106. 'show_ui' => true,
  107. 'query_var' => true,
  108. )
  109. );
  110. /*
  111. looking for custom meta boxes?
  112. check out this fantastic tool:
  113. https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
  114. */
  115. ?>