Custom Post Types and Taxonomies In WordPress Brad Williams WebDevStudios.com
Who Am I?
Brad Williams Co-Founder of WebDevStudios.com Organizer NJ WordPress Meetup Co-Host SitePoint Podcast Co-Author of  Profes...
<ul><li>Explain Custom Post Types and Taxonomies </li></ul><ul><li>Create Custom Post Types </li></ul><ul><li>Create Custo...
So what are  Custom Post Types?
So what are  Custom Post Types? CODEX : Post type refers to the various structured data that is maintained in the WordPres...
WTFrack?
So what are  Custom Post Types Really? ENGLISH : Custom Post Types allow you to create different types of content in WordP...
Default WordPress Post Types: <ul><li>Posts </li></ul><ul><li>Pages </li></ul><ul><li>Attachments </li></ul><ul><li>Revisi...
Custom Post Type Ideas <ul><li>Podcasts </li></ul><ul><li>Movies </li></ul><ul><li>Bars </li></ul><ul><li>Forum </li></ul>...
Example Time! Tweet: @williamsba CAUTION ZOMBIES AHEAD! #wcraleigh Win a copy of Professional WordPress!
Example: Zombie Modeling Agency
Example: Zombie Modeling Agency <?php add_action( 'init', 'create_zombie_post_type' ); function create_zombie_post_type() ...
Example: Zombie Modeling Agency Drop the below code in your themes functions.php file
Example: Zombie Modeling Agency <?php add_action( 'init', 'create_zombie_post_type' ); function create_zombie_post_type() ...
Additional Arguments:  labels An array of strings that represent your post type in the WP admin name: The plural form of t...
Additional Arguments:  labels register_post_type('zombies',  array( 'labels' => array( 'name' => 'Zombies', 'singular_name...
Additional Arguments:  labels The Result
Additional Arguments:  supports Defines what meta boxes and other fields appear on the edit screen title: Text input field...
Additional Arguments:  supports Defines what meta boxes and other fields appear on the edit screen register_post_type('zom...
Additional Arguments:  supports Defines what meta boxes and other fields appear on the edit screen Only the Title and Publ...
Additional Arguments:  supports Now lets activate all meta boxes register_post_type('zombies',  array( 'labels' => array( ...
Additional Arguments:  supports Now all meta boxes are displayed on the Zombie edit screen
Additional Arguments:  rewrite Defines the permalink structure of your custom post type posts slug: The slug to use in you...
Additional Arguments:  taxonomies Add preexisting taxonomies to your custom post type 'taxonomies' => array( 'post_tag', '...
Additional Arguments:  misc Below are additional arguments for creating custom post types hierarchical: whether the post t...
Putting it all together register_post_type('zombies',  array( 'description' => 'Zombie custom post type', 'show_ui' => tru...
So what are Taxonomies?
So what are Taxonomies? ENGLISH : Taxonomies are a way to group similar items together
Default WordPress Taxonomies: <ul><li>Category </li></ul><ul><li>Tag </li></ul><ul><li>Link Category </li></ul>
Example Time! Tweet: @williamsba ZOMBIES IN AREA! RUN #wcraleigh Win a copy of Professional WordPress!
Example: Zombie Modeling Agency <?php add_action( 'init', 'create_zombie_taxonomies' ); function create_zombie_taxonomies(...
Example: Zombie Modeling Agency New custom taxonomy is automatically added to the Zombies post type menu The Freshness met...
Example: Zombie Modeling Agency <?php add_action( 'init', 'create_zombie_taxonomies' ); function create_zombie_taxonomies(...
Example: Zombie Modeling Agency New custom taxonomy is automatically added to the Zombies post type menu The Freshness met...
Example: Zombie Modeling Agency Lets break it down: <ul><li>Action hook to trigger our function </li></ul><ul><li>Execute ...
Additional Arguments: labels An array of strings that represent your taxonomy in the WP admin name: The general name of yo...
Additional Arguments: labels register_taxonomy(  'freshness',  'zombies', array(  'labels' => array( 'name' => 'Freshness'...
Additional Arguments: labels The Result
Additional Arguments: rewrite Defines the permalink structure for your custom taxonomy slug: The slug to use in your perma...
Additional Arguments: misc Below are additional arguments for creating custom taxonomies public: whether the taxonomy is p...
Putting it all together register_taxonomy(  'freshness',  'zombies', array(  'labels' => array( 'name' => 'Freshness', 'si...
 
Displaying in WordPress  #protip
Displaying Custom Post Type Content <?php query_posts( array( 'post_type' => 'zombies' ) ); ?> By default custom post type...
Displaying Custom Post Type Content <?php query_posts( array( 'post_type' => 'zombies' ) ); ?> BEFORE ZOMBIED (After)
Custom Loop <?php  $zombies = new WP_Query( array( 'post_type' => 'zombies', 'posts_per_page' => 1, 'orderby' => 'rand' ) ...
<?php  if (have_posts()) : while (have_posts()) : the_post();  $post_type = get_post_type( get_the_ID() ); if ($post_type ...
if ( is_post_type( 'zombies' ) ) { echo 'Zombies Exist!'; } Function: is_post_type() Check if a post type exists Custom Po...
<?php echo get_the_term_list( $post->ID, 'freshness', 'Freshness: ', ', ', '' ); ?> Function: get_the_term_list() Display ...
Recommended Plugin
Custom Post Type UI http://wordpress.org/extend/plugins/custom-post-type-ui/ Easily create custom post types without writi...
Custom Post Type UI http://wordpress.org/extend/plugins/custom-post-type-ui/ Also easily create custom taxonomies without ...
Custom Post Type UI http://wordpress.org/extend/plugins/custom-post-type-ui/ Plugin also gives you the PHP code for custom...
<ul><li>Related Codex Articles </li></ul><ul><ul><li>http://codex.wordpress.org/Hardening_WordPress </li></ul></ul><ul><li...
Brad Williams [email_address] Blog: strangework.com Twitter: @williamsba IRC: WDS-Brad Contact Professional WordPress: htt...
Upcoming SlideShare
Loading in...5
×

Custom Post Types and Taxonomies in WordPress

59,258

Published on

My WordCamp Raleigh presentation on custom post types and taxonomies in WordPress

14 Comments
53 Likes
Statistics
Notes
No Downloads
Views
Total Views
59,258
On Slideshare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
716
Comments
14
Likes
53
Embeds 0
No embeds

No notes for slide

Custom Post Types and Taxonomies in WordPress

  1. 1. Custom Post Types and Taxonomies In WordPress Brad Williams WebDevStudios.com
  2. 2. Who Am I?
  3. 3. Brad Williams Co-Founder of WebDevStudios.com Organizer NJ WordPress Meetup Co-Host SitePoint Podcast Co-Author of Professional WordPress (http://bit.ly/pro-wp) Who Am I?
  4. 4. <ul><li>Explain Custom Post Types and Taxonomies </li></ul><ul><li>Create Custom Post Types </li></ul><ul><li>Create Custom Taxonomies </li></ul><ul><li>Hooking into WordPress </li></ul>Topics
  5. 5. So what are Custom Post Types?
  6. 6. So what are Custom Post Types? CODEX : Post type refers to the various structured data that is maintained in the WordPress posts table. Native (or built-in) post type are  post ,  page ,  attachment ,  revision , and  nav-menu-item . Custom post types are also supported in WordPress and can be defined with  register_post_type() .
  7. 7. WTFrack?
  8. 8. So what are Custom Post Types Really? ENGLISH : Custom Post Types allow you to create different types of content in WordPress.
  9. 9. Default WordPress Post Types: <ul><li>Posts </li></ul><ul><li>Pages </li></ul><ul><li>Attachments </li></ul><ul><li>Revisions </li></ul><ul><li>Nav Menus (WP 3.0) </li></ul>
  10. 10. Custom Post Type Ideas <ul><li>Podcasts </li></ul><ul><li>Movies </li></ul><ul><li>Bars </li></ul><ul><li>Forum </li></ul><ul><li>Quotes </li></ul><ul><li>Videos </li></ul><ul><li>Cars </li></ul><ul><li>House Listings </li></ul><ul><li>Events </li></ul><ul><li>Ticket System </li></ul><ul><li>etc, etc, etc </li></ul>The possibilities are endless!
  11. 11. Example Time! Tweet: @williamsba CAUTION ZOMBIES AHEAD! #wcraleigh Win a copy of Professional WordPress!
  12. 12. Example: Zombie Modeling Agency
  13. 13. Example: Zombie Modeling Agency <?php add_action( 'init', 'create_zombie_post_type' ); function create_zombie_post_type() { register_post_type('zombies', array( 'label' => 'Zombies', 'public' => true, ) ); } ?> Drop the below code in your themes functions.php file
  14. 14. Example: Zombie Modeling Agency Drop the below code in your themes functions.php file
  15. 15. Example: Zombie Modeling Agency <?php add_action( 'init', 'create_zombie_post_type' ); function create_zombie_post_type() { register_post_type('zombies', array( 'label' => 'Zombies', 'public' => true, ) ); } ?> Lets break it down: 1. Action hook to trigger our function 2. Execute the register_post_type function defining zombies as our custom post type 3. Set the label to Zombies 4. Set public to true. By default public is false which will hide your post type
  16. 16. Additional Arguments: labels An array of strings that represent your post type in the WP admin name: The plural form of the name of your post type. singular_name: The singular form of the name of your post type. add_new: The menu item for adding a new post. add_new_item: The header shown when creating a new post. edit: The menu item for editing posts. edit_item: The header shown when editing a post. new_item: Shown in the favorites menu in the admin header. view: Used as text in a link to view the post. view_item: Shown alongside the permalink on the edit post screen. search_items: Button text for the search box on the edit posts screen. not_found: Text to display when no posts are found through search in the admin. not_found_in_trash: Text to display when no posts are in the trash. parent: Used as a label for a parent post on the edit posts screen. Only useful for hierarchical post types. Ref: http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress
  17. 17. Additional Arguments: labels register_post_type('zombies', array( 'labels' => array( 'name' => 'Zombies', 'singular_name' => 'Zombie', 'add_new' => 'Add New Zombie', 'add_new_item' => 'Add New Zombie', 'edit' => 'Edit Zombies', 'edit_item' => 'Edit Zombie', 'new_item' => 'New Zombie', 'view' => 'View Zombie', 'view_item' => 'View Zombie', 'search_items' => 'Search Zombies', 'not_found' => 'No zombies found', 'not_found_in_trash' => 'No zombies found in Trash', 'parent' => 'Parent Zombie', ), 'public' => true, ) ); An array of strings that represent your post type in the WP admin
  18. 18. Additional Arguments: labels The Result
  19. 19. Additional Arguments: supports Defines what meta boxes and other fields appear on the edit screen title: Text input field for the post title. editor: Main content meta box comments: Ability to turn comments on/off. trackbacks: Ability to turn trackbacks and pingbacks on/off. revisions: Allows revisions to be made of your post. author: Displays the post author select box. excerpt: A textarea for writing a custom excerpt. thumbnail: The post thumbnail (featured imaged) upload box. custom-fields: Custom fields input area. page-attributes: The attributes box shown for pages. Important for hierarchical post types, so you can select the parent post.
  20. 20. Additional Arguments: supports Defines what meta boxes and other fields appear on the edit screen register_post_type('zombies', array( 'labels' => array( 'name' => 'Zombies', 'singular_name' => 'Zombie', 'add_new' => 'Add New Zombie', 'add_new_item' => 'Add New Zombie', 'edit' => 'Edit Zombies', 'edit_item' => 'Edit Zombie', 'new_item' => 'New Zombie', 'view' => 'View Zombie', 'view_item' => 'View Zombie', 'search_items' => 'Search Zombies', 'not_found' => 'No zombies found', 'not_found_in_trash' => 'No zombies found in Trash', 'parent' => 'Parent Zombie', ), 'supports' => array('title'), 'public' => true, ) );
  21. 21. Additional Arguments: supports Defines what meta boxes and other fields appear on the edit screen Only the Title and Publish meta boxes are displayed
  22. 22. Additional Arguments: supports Now lets activate all meta boxes register_post_type('zombies', array( 'labels' => array( 'name' => 'Zombies', 'singular_name' => 'Zombie', 'add_new' => 'Add New Zombie', 'add_new_item' => 'Add New Zombie', 'edit' => 'Edit Zombies', 'edit_item' => 'Edit Zombie', 'new_item' => 'New Zombie', 'view' => 'View Zombie', 'view_item' => 'View Zombie', 'search_items' => 'Search Zombies', 'not_found' => 'No zombies found', 'not_found_in_trash' => 'No zombies found in Trash', 'parent' => 'Parent Zombie', ), 'supports' => array('title‘,'editor‘,'excerpt‘, 'trackbacks‘,'custom-fields', 'comments‘,'revisions‘,'thumbnail‘,'author‘,'page-attributes'), 'public' => true, ) );
  23. 23. Additional Arguments: supports Now all meta boxes are displayed on the Zombie edit screen
  24. 24. Additional Arguments: rewrite Defines the permalink structure of your custom post type posts slug: The slug to use in your permalink structure with_front: whether your post type should use the front base from your permalink settings (eg /blog) 'rewrite' => array('slug' => 'zombie') Example: BEFORE AFTER
  25. 25. Additional Arguments: taxonomies Add preexisting taxonomies to your custom post type 'taxonomies' => array( 'post_tag', 'category') Example:
  26. 26. Additional Arguments: misc Below are additional arguments for creating custom post types hierarchical: whether the post type is hierarchical description: a text description of your custom post type show_ui: whether to show the admin menus/screens menu_position: Set the position of the menu order where the post type should appear menu_icon: URL to the menu icon to be used exclude_from_search: whether post type content should appear in search results
  27. 27. Putting it all together register_post_type('zombies', array( 'description' => 'Zombie custom post type', 'show_ui' => true, 'menu_position' => 5, 'menu_icon' => get_stylesheet_directory_uri() . '/images/zombies.png', 'exclude_from_search' => false, 'labels' => array( 'name' => 'Zombies', 'singular_name' => 'Zombie', 'add_new' => 'Add New Zombie', 'add_new_item' => 'Add New Zombie', 'edit' => 'Edit Zombies', 'edit_item' => 'Edit Zombie', 'new_item' => 'New Zombie', 'view' => 'View Zombie', 'view_item' => 'View Zombie', 'search_items' => 'Search Zombies', 'not_found' => 'No zombies found', 'not_found_in_trash' => 'No zombies found in Trash', 'parent' => 'Parent Zombie', ), 'supports' => array('title','editor','excerpt', 'trackbacks','custom-fields', 'comments','revisions','thumbnail','author','page-attributes'), 'public' => true, 'rewrite' => array('slug' => 'zombie', 'with_front' => false), 'taxonomies' => array( 'post_tag', 'category') ) );
  28. 28. So what are Taxonomies?
  29. 29. So what are Taxonomies? ENGLISH : Taxonomies are a way to group similar items together
  30. 30. Default WordPress Taxonomies: <ul><li>Category </li></ul><ul><li>Tag </li></ul><ul><li>Link Category </li></ul>
  31. 31. Example Time! Tweet: @williamsba ZOMBIES IN AREA! RUN #wcraleigh Win a copy of Professional WordPress!
  32. 32. Example: Zombie Modeling Agency <?php add_action( 'init', 'create_zombie_taxonomies' ); function create_zombie_taxonomies() { register_taxonomy( 'freshness', 'zombies', array( 'hierarchical' => false, 'label' => 'Freshness' ) ); } ?> Drop the below code in your themes functions.php file Non-hierarchical Taxonomy
  33. 33. Example: Zombie Modeling Agency New custom taxonomy is automatically added to the Zombies post type menu The Freshness meta box is also automatically added to the Zombie edit screen automatically!
  34. 34. Example: Zombie Modeling Agency <?php add_action( 'init', 'create_zombie_taxonomies' ); function create_zombie_taxonomies() { register_taxonomy( 'freshness', 'zombies', array( 'hierarchical' => true, 'label' => 'Freshness' ) ); } ?> Drop the below code in your themes functions.php file Hierarchical Taxonomy
  35. 35. Example: Zombie Modeling Agency New custom taxonomy is automatically added to the Zombies post type menu The Freshness meta box is also automatically added to the Zombie edit screen automatically!
  36. 36. Example: Zombie Modeling Agency Lets break it down: <ul><li>Action hook to trigger our function </li></ul><ul><li>Execute the register_taxonomy function defining freshness as our custom taxonomy </li></ul><ul><li>Set the name to freshness </li></ul><ul><li>Set the object type to ‘zombies’. Must be an existing post type </li></ul><ul><li>Set hierarchical to false </li></ul><ul><li>Set our taxonomy label to Freshness </li></ul><?php add_action( 'init', 'create_zombie_taxonomies' ); function create_zombie_taxonomies() { register_taxonomy( 'freshness', 'zombies', array( 'hierarchical' => false, 'label' => 'Freshness' ) ); } ?>
  37. 37. Additional Arguments: labels An array of strings that represent your taxonomy in the WP admin name: The general name of your taxonomy singular_name: The singular form of the name of your taxonomy. search_items: The search items text popular_items: The popular items text all_items: The all items text parent_item: The parent item text. Only used on hierarchical taxonomies parent_item_colon: Same as parent_item, but with a colon edit_item: The edit item text update_item: The update item text add_new_item: The add new item text new_item_name: The new item name text
  38. 38. Additional Arguments: labels register_taxonomy( 'freshness', 'zombies', array( 'labels' => array( 'name' => 'Freshness', 'singular_name' => 'Freshness', 'search_items' => 'Search Freshness', 'popular_items' => 'Popular Freshness', 'all_items' => 'All Freshness', 'parent_item' => 'Parent Freshness', 'parent_item_colon' => 'Parent Freshness:', 'edit_item' => 'Edit Freshness', 'update_item' => 'Update Freshness', 'add_new_item' => 'Add New Freshness', 'new_item_name' => 'New Freshness Name' ), 'hierarchical' => false, 'label' => 'Freshness' ) ); An array of strings that represent your taxonomy in the WP admin
  39. 39. Additional Arguments: labels The Result
  40. 40. Additional Arguments: rewrite Defines the permalink structure for your custom taxonomy slug: The slug to use in your permalink structure with_front: whether your taxonomy should use the front base from your permalink settings (eg /blog) 'rewrite' => array('slug' => fresh') Example: BEFORE AFTER http://x.webdevstudios.com/blog/freshness/ripe/ http://x.webdevstudios.com/blog/fresh/ripe/
  41. 41. Additional Arguments: misc Below are additional arguments for creating custom taxonomies public: whether the taxonomy is publicly queryable show_ui: whether to display the admin UI for the taxonomy hierarchical: whether the taxonomy is hierarchical. Categories are hierarchical, tags are not. label: the name of your taxonomy in the admin dashboard query_var: whether to be able to query posts using the taxonomy. rewrite: whether to use a pretty permalink when viewing the taxonomy page. show_tagcloud: whether to show a tag cloud in the admin UI
  42. 42. Putting it all together register_taxonomy( 'freshness', 'zombies', array( 'labels' => array( 'name' => 'Freshness', 'singular_name' => 'Freshness', 'search_items' => 'Search Freshness', 'popular_items' => 'Popular Freshness', 'all_items' => 'All Freshness', 'parent_item' => 'Parent Freshness', 'parent_item_colon' => 'Parent Freshness:', 'edit_item' => 'Edit Freshness', 'update_item' => 'Update Freshness', 'add_new_item' => 'Add New Freshness', 'new_item_name' => 'New Freshness Name' ), 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'query_var' => 'freshness', 'show_tagcloud' => true, 'rewrite' => array( 'slug' => 'fresh', 'with_front' => false ) ) );
  43. 44. Displaying in WordPress #protip
  44. 45. Displaying Custom Post Type Content <?php query_posts( array( 'post_type' => 'zombies' ) ); ?> By default custom post type content will NOT display in the Loop Placing the above code directly before the Loop will only display our zombies
  45. 46. Displaying Custom Post Type Content <?php query_posts( array( 'post_type' => 'zombies' ) ); ?> BEFORE ZOMBIED (After)
  46. 47. Custom Loop <?php $zombies = new WP_Query( array( 'post_type' => 'zombies', 'posts_per_page' => 1, 'orderby' => 'rand' ) ); while ( $zombies->have_posts() ) : $zombies->the_post(); the_title( '<h2><a href=&quot;' . get_permalink() . '&quot; >', '</a></h2>' ); ?> <div class=&quot;entry-content&quot;> <?php the_content(); ?> </div> <?php endwhile; ?> Creating a custom Loop for your post type
  47. 48. <?php if (have_posts()) : while (have_posts()) : the_post(); $post_type = get_post_type( get_the_ID() ); if ($post_type == 'zombies') { echo 'This is a Zombie!'; } ?> <?php endwhile; ?> <?php endif; ?> Function: get_post_type() Returns the post type of the content you are viewing Custom Post Type Functions http://codex.wordpress.org/Function_Reference/get_post_type
  48. 49. if ( is_post_type( 'zombies' ) ) { echo 'Zombies Exist!'; } Function: is_post_type() Check if a post type exists Custom Post Type Functions if ( is_post_type( 'zombies' $post_id) ) { echo ‘Post ID: ‘ .$post_id .‘is a Zombie!'; } Can also check a specific post against a post type
  49. 50. <?php echo get_the_term_list( $post->ID, 'freshness', 'Freshness: ', ', ', '' ); ?> Function: get_the_term_list() Display freshness taxonomy for our zombies Custom Taxonomy Functions http://codex.wordpress.org/Function_Reference/get_the_term_list
  50. 51. Recommended Plugin
  51. 52. Custom Post Type UI http://wordpress.org/extend/plugins/custom-post-type-ui/ Easily create custom post types without writing code!
  52. 53. Custom Post Type UI http://wordpress.org/extend/plugins/custom-post-type-ui/ Also easily create custom taxonomies without writing code!
  53. 54. Custom Post Type UI http://wordpress.org/extend/plugins/custom-post-type-ui/ Plugin also gives you the PHP code for custom post types and taxonomies!
  54. 55. <ul><li>Related Codex Articles </li></ul><ul><ul><li>http://codex.wordpress.org/Hardening_WordPress </li></ul></ul><ul><li>Custom Post Type Articles </li></ul><ul><ul><li>http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress </li></ul></ul><ul><ul><li>http://justintadlock.com/archives/2010/02/02/showing-custom-post-types-on-your-home-blog-page </li></ul></ul><ul><ul><li>http://kovshenin.com/archives/custom-post-types-in-wordpress-3-0/ </li></ul></ul><ul><ul><li>http://wpengineer.com/impressions-of-custom-post-type/ </li></ul></ul><ul><li>Custom Taxonomies </li></ul><ul><ul><li>http://justintadlock.com/archives/2009/05/06/custom-taxonomies-in-wordpress-28 </li></ul></ul><ul><ul><li>http://justintadlock.com/archives/2009/06/04/using-custom-taxonomies-to-create-a-movie-database </li></ul></ul><ul><ul><li>http://www.shibashake.com/wordpress-theme/wordpress-custom-taxonomy-input-panels </li></ul></ul>Custom Post Type and Taxonomy Resources
  55. 56. Brad Williams [email_address] Blog: strangework.com Twitter: @williamsba IRC: WDS-Brad Contact Professional WordPress: http://bit.ly/pro-wp
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×