Wordpress register Custom Post Type and Custom Taxonomy


/ Published in: PHP
Save to your folder(s)

Register a Custom Post Type and a Custom Taxonomy in Wordpress


Copy this code and paste it in your HTML
  1. /** -------------- ADD CUSTOM POST TYPES ----------------- **/
  2.  
  3. // The register_post_type() function is not to be used before the 'init'.
  4.  
  5. add_action( 'init', 'my_custom_init' );
  6.  
  7. /* Here's how to create your customized labels */
  8.  
  9. function my_custom_init() {
  10.  
  11. $labels = array(
  12. 'name' => _x( 'Case Studies', 'post type general name' ), // Tip: _x('') is used for localization
  13. 'singular_name' => _x( 'Case Study', 'post type singular name' ),
  14. 'add_new' => _x( 'Add New', 'book' ),
  15. 'add_new_item' => __( 'Add New Case Study' ),
  16. 'edit_item' => __( 'Edit Case Study' ),
  17. 'new_item' => __( 'New Case Study' ),
  18. 'view_item' => __( 'View Case Study' ),
  19. 'search_items' => __( 'Search Case Studies' ),
  20. 'not_found' => __( 'No Case Studies found' ),
  21. 'not_found_in_trash' => __( 'No Case Studies found in Trash' ),
  22. 'parent_item_colon' => ''
  23. );
  24.  
  25. // Create an array for the $args
  26.  
  27. $args = array( 'labels' => $labels, /* NOTICE: the $labels variable is used here... */
  28. 'public' => true,
  29. 'publicly_queryable' => true,
  30. 'show_ui' => true,
  31. 'show_in_nav_menus' => true,
  32. 'query_var' => true,
  33. 'rewrite' => true,
  34. 'capability_type' => 'post',
  35. 'hierarchical' => true,
  36. 'menu_position' => null,
  37. 'has_archive' => true,
  38. 'rewrite' => true,
  39. 'taxonomies' => array( 'Project Type' ),
  40. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'page-attributes' )
  41. );
  42.  
  43. register_post_type( 'case-studies', $args ); /* Register it and move on */
  44. }
  45.  
  46.  
  47. /** -------------- REGISTER CUSTOM TAXONOMIES ----------------- **/
  48.  
  49.  
  50. //hook into the init action and call create_book_taxonomies when it fires
  51. add_action( 'init', 'create_casestudies_taxonomies', 0 );
  52.  
  53. //create two taxonomies, genres and writers for the post type "book"
  54. function create_casestudies_taxonomies()
  55. {
  56. // Add new taxonomy, make it hierarchical (like categories)
  57. $labels = array(
  58. 'name' => _x( 'Project Type', 'taxonomy general name' ),
  59. 'singular_name' => _x( 'Project Type', 'taxonomy singular name' ),
  60. 'search_items' => __( 'Search Project Types' ),
  61. 'all_items' => __( 'All Project Types' ),
  62. 'parent_item' => __( 'Parent Project Type' ),
  63. 'parent_item_colon' => __( 'Parent Project Type:' ),
  64. 'edit_item' => __( 'Edit Project Type' ),
  65. 'update_item' => __( 'Update Project Type' ),
  66. 'add_new_item' => __( 'Add New Project Type' ),
  67. 'new_item_name' => __( 'New Project Type Name' ),
  68. 'menu_name' => __( 'Project Type' ),
  69. );
  70.  
  71. register_taxonomy('genre',array('case-studies'), array(
  72. 'hierarchical' => true,
  73. 'labels' => $labels,
  74. 'show_ui' => true,
  75. 'query_var' => true,
  76. 'rewrite' => array( 'slug' => 'project' ),
  77. ));
  78.  
  79. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.