WordPress 3 Custom Post Types

5,124 views

Published on

Using Custom Post Types with WordPress 3. Presented at WordCamp Victoria BC, May 15, 2010.

Published in: Technology
0 Comments
11 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,124
On SlideShare
0
From Embeds
0
Number of Embeds
262
Actions
Shares
0
Downloads
0
Comments
0
Likes
11
Embeds 0
No embeds

No notes for slide


































































  • WordPress 3 Custom Post Types

    1. 1. Custom Post types in WordPress 3 Dave Zille May 15, 2010 - WordCamp Victoria BC
    2. 2. About Dave • President, dazil Internet Services • WordPress dev, WordPress conversions • web: dazil.com twitter: @dazil • Principal, Learn it Today • WordPress classroom and online training • web: learnittoday.ca twitter: @learnittodayca
    3. 3. Agenda • Custom Post Types: • What are they? What aren’t they? • Why do we need them? • Custom Post Type ideas/examples • Demo: • Creating a custom post type • Displaying a custom post type • Resources
    4. 4. WordPress 3: Not just for Blogs Anymore! The term “blog” has been replaced with “site” throughout WordPress 3 is (officially) an actual CMS! WordPress install process now asks for “Site Title”
    5. 5. Top 5 Reasons WP is a CMS:
    6. 6. Top 5 Reasons WP is a CMS: 1. Scalability
    7. 7. Top 5 Reasons WP is a CMS: 1. Scalability 2. Security
    8. 8. Top 5 Reasons WP is a CMS: 1. Scalability 2. Security 3. Menu Management
    9. 9. Top 5 Reasons WP is a CMS: 1. Scalability 2. Security 3. Menu Management 4. Custom Taxonomies
    10. 10. Top 5 Reasons WP is a CMS: 1. Scalability 2. Security 3. Menu Management 4. Custom Taxonomies 5. Custom Post Types
    11. 11. What are Custom Post Types? • Custom Post Types are “content” types • Not to be confused with “blog post” • Traditional “post” is actually just another Custom Post Type • Can be used to store and administer different types of content on your WordPress site • Traditionally done via plugins (Flutter, Pods, etc) • A huge part of why WP 3 is a CMS
    12. 12. What aren’t Custom Post Types? • Custom Post Types are not: • A replacement for Custom Fields • (used in conjunction with Custom Fields) • Completely GUI driven • (i.e. cannot be created with default WP GUI)
    13. 13. Why do we need Custom Post Types? • Because Custom Post Types: • Make it easy to create and edit different forms of content within WordPress • Eliminate the need to “fake” custom post types by using 3rd party plugins • Will make you and your clients happy!
    14. 14. Custom Post Type Examples • Media: • Video, Podcasts • Title,YouTube URL, Length/Duration, Captions, Show Notes, etc • Information: • Car for Sale • Make, Model, Color, Features, Pictures, etc..
    15. 15. Custom Post Type Examples • Information (cont’d) • Real Estate Listing • Price, # bed, # bath, amenities, photos, etc. • Gallery/Portfolio • Thumbnail, description, URL, etc. • Calendar of Events • Date, time, cost, location, etc.
    16. 16. Your First Custom Post Type
    17. 17. Your First Custom Post Type • Case Study:
    18. 18. Your First Custom Post Type • Case Study: • Recipe database
    19. 19. Your First Custom Post Type • Case Study: • Recipe database • Description
    20. 20. Your First Custom Post Type • Case Study: • Recipe database • Description • Ingredients
    21. 21. Your First Custom Post Type • Case Study: • Recipe database • Description • Ingredients • Prep Time
    22. 22. Your First Custom Post Type • Case Study: • Recipe database • Description • Ingredients • Prep Time • Cook Time
    23. 23. Custom Post Types: The Code • The register_post_type() function was introduced in WordPress 2.9 • WordPress 3 makes register_post_type() very useful • Minimal code, and a Custom Post Type is up and running: add_action( 'init', 'create_recipe_post_type' ); function create_recipe_post_type() { register_post_type( 'recipe', array( 'label' => __( 'Recipes' ), 'singular_label' => __( 'Recipe' ), 'public' => true, ) ); }
    24. 24. Custom Post Types: The Code
    25. 25. Custom Post Types: The Code • Where does the code go? • 2 options: • Create a plugin file, or • add to your theme’s functions.php
    26. 26. Custom Post Types: The Code • Where does the code go? • 2 options: • Create a plugin file, or • add to your theme’s functions.php • This is useful, but can I do more? • The register_post_type() function has 20+ arguments • Can control a lot about CP Types using them • Let’s review some of them..
    27. 27. Custom Post Types: The Code
    28. 28. Custom Post Types: The Code register_post_type() arguments • label • A plural descriptive name for the post type • eg “Recipes” • singular_label • A singular descriptive name for the post type • eg “Recipe”
    29. 29. Custom Post Types: The Code
    30. 30. Custom Post Types: The Code register_post_type() arguments • description • A short descriptive summary of what the post type is • eg. “A set of directions with a list of ingredients for making or preparing food.” • public • Whether or not the post type should be made available in the admin • boolean, default: false
    31. 31. Custom Post Types: The Code
    32. 32. Custom Post Types: The Code register_post_type() arguments • menu_position • Allows the positioning of the post type in the admin menu • Default: a new post type is added after Comments • menu_icon • Allows you to specify a custom icon for the post type • Default: posts icon
    33. 33. Custom Post Types: The Code
    34. 34. Custom Post Types: The Code register_post_type() arguments
    35. 35. Custom Post Types: The Code register_post_type() arguments • hierarchical
    36. 36. Custom Post Types: The Code register_post_type() arguments • hierarchical • Determines whether the post type is hierarchical (as in ‘pages’), or not (as in ‘posts’)
    37. 37. Custom Post Types: The Code register_post_type() arguments • hierarchical • Determines whether the post type is hierarchical (as in ‘pages’), or not (as in ‘posts’) • Default: false
    38. 38. Custom Post Types: The Code register_post_type() arguments • hierarchical • Determines whether the post type is hierarchical (as in ‘pages’), or not (as in ‘posts’) • Default: false • can_export
    39. 39. Custom Post Types: The Code register_post_type() arguments • hierarchical • Determines whether the post type is hierarchical (as in ‘pages’), or not (as in ‘posts’) • Default: false • can_export • Specifies whether posts of the post type can be exportable using WordPress’ export function
    40. 40. Custom Post Types: The Code register_post_type() arguments • hierarchical • Determines whether the post type is hierarchical (as in ‘pages’), or not (as in ‘posts’) • Default: false • can_export • Specifies whether posts of the post type can be exportable using WordPress’ export function • Default: true
    41. 41. Custom Post Types: The Code
    42. 42. Custom Post Types: The Code register_post_type() arguments
    43. 43. Custom Post Types: The Code register_post_type() arguments • supports
    44. 44. Custom Post Types: The Code register_post_type() arguments • supports • Defines what meta boxes and other fields appear when editing or creating a post
    45. 45. Custom Post Types: The Code register_post_type() arguments • supports • Defines what meta boxes and other fields appear when editing or creating a post • Options: title, editor, comments, trackbacks, revisions, author, excerpt, thumbnail, custom- fields, page-attributes
    46. 46. Custom Post Types: The Code register_post_type() arguments • supports • Defines what meta boxes and other fields appear when editing or creating a post • Options: title, editor, comments, trackbacks, revisions, author, excerpt, thumbnail, custom- fields, page-attributes • Default: title, editor
    47. 47. Custom Post Types: The Code
    48. 48. Custom Post Types: The Code register_post_type() arguments
    49. 49. Custom Post Types: The Code register_post_type() arguments • register_meta_box_cb
    50. 50. Custom Post Types: The Code register_post_type() arguments • register_meta_box_cb • taxonomies
    51. 51. Custom Post Types: The Code register_post_type() arguments • register_meta_box_cb • taxonomies • capability_type / capabilities
    52. 52. Custom Post Types: Displaying
    53. 53. Custom Post Types: Displaying • Customizing the custom post type template
    54. 54. Custom Post Types: Displaying • Customizing the custom post type template • single.php
    55. 55. Custom Post Types: Displaying • Customizing the custom post type template • single.php • template for the custom post type (default)
    56. 56. Custom Post Types: Displaying • Customizing the custom post type template • single.php • template for the custom post type (default) • single-post_type_name.php
    57. 57. Custom Post Types: Displaying • Customizing the custom post type template • single.php • template for the custom post type (default) • single-post_type_name.php • custom template for the custom post type
    58. 58. Custom Post Types: Displaying • Customizing the custom post type template • single.php • template for the custom post type (default) • single-post_type_name.php • custom template for the custom post type • eg. single-recipe.php
    59. 59. Custom Post Types: Displaying
    60. 60. Custom Post Types: Displaying • Displaying custom post types on your site’s homepage
    61. 61. Custom Post Types: Displaying • Displaying custom post types on your site’s homepage • Add to your theme’s functions.php:
    62. 62. Custom Post Types: Displaying • Displaying custom post types on your site’s homepage • Add to your theme’s functions.php: add_filter( 'pre_get_posts', 'my_get_posts' );
    63. 63. Custom Post Types: Displaying • Displaying custom post types on your site’s homepage • Add to your theme’s functions.php: add_filter( 'pre_get_posts', 'my_get_posts' ); function my_get_posts( $query ) {
    64. 64. Custom Post Types: Displaying • Displaying custom post types on your site’s homepage • Add to your theme’s functions.php: add_filter( 'pre_get_posts', 'my_get_posts' ); function my_get_posts( $query ) { if ( is_home() )
    65. 65. Custom Post Types: Displaying • Displaying custom post types on your site’s homepage • Add to your theme’s functions.php: add_filter( 'pre_get_posts', 'my_get_posts' ); function my_get_posts( $query ) { if ( is_home() ) $query->set( 'post_type', array( 'post', 'recipe' ) );
    66. 66. Custom Post Types: Displaying • Displaying custom post types on your site’s homepage • Add to your theme’s functions.php: add_filter( 'pre_get_posts', 'my_get_posts' ); function my_get_posts( $query ) { if ( is_home() ) $query->set( 'post_type', array( 'post', 'recipe' ) ); return $query;
    67. 67. Custom Post Types: Displaying • Displaying custom post types on your site’s homepage • Add to your theme’s functions.php: add_filter( 'pre_get_posts', 'my_get_posts' ); function my_get_posts( $query ) { if ( is_home() ) $query->set( 'post_type', array( 'post', 'recipe' ) ); return $query; }
    68. 68. Other Custom Post Type Functions
    69. 69. Other Custom Post Type Functions • Get the “post type” of a post:
    70. 70. Other Custom Post Type Functions • Get the “post type” of a post: • get_post_type() allows you to check the post type of a specific post
    71. 71. Other Custom Post Type Functions • Get the “post type” of a post: • get_post_type() allows you to check the post type of a specific post $post_type = get_post_type($post_id);
    72. 72. Other Custom Post Type Functions • Get the “post type” of a post: • get_post_type() allows you to check the post type of a specific post $post_type = get_post_type($post_id); • Check if a post is of a specific type:
    73. 73. Other Custom Post Type Functions • Get the “post type” of a post: • get_post_type() allows you to check the post type of a specific post $post_type = get_post_type($post_id); • Check if a post is of a specific type: • is_post_type() allows you to check a specific post against a specific post type
    74. 74. Other Custom Post Type Functions • Get the “post type” of a post: • get_post_type() allows you to check the post type of a specific post $post_type = get_post_type($post_id); • Check if a post is of a specific type: • is_post_type() allows you to check a specific post against a specific post type if ( is_post_type( 'recipe', $post_id ) )
    75. 75. Other Custom Post Type Functions • Get the “post type” of a post: • get_post_type() allows you to check the post type of a specific post $post_type = get_post_type($post_id); • Check if a post is of a specific type: • is_post_type() allows you to check a specific post against a specific post type if ( is_post_type( 'recipe', $post_id ) ) echo 'This is a not a blog post. It is a recipe.';
    76. 76. Other Custom Post Type Functions • Get the “post type” of a post: • get_post_type() allows you to check the post type of a specific post $post_type = get_post_type($post_id); • Check if a post is of a specific type: • is_post_type() allows you to check a specific post against a specific post type if ( is_post_type( 'recipe', $post_id ) ) echo 'This is a not a blog post. It is a recipe.'; else
    77. 77. Other Custom Post Type Functions • Get the “post type” of a post: • get_post_type() allows you to check the post type of a specific post $post_type = get_post_type($post_id); • Check if a post is of a specific type: • is_post_type() allows you to check a specific post against a specific post type if ( is_post_type( 'recipe', $post_id ) ) echo 'This is a not a blog post. It is a recipe.'; else echo 'This is not a recipe.';
    78. 78. Resources • WordPress.org Codex: register_post_type function reference: • http://codex.wordpress.org/Function_Reference/register_post_type • Blog post: Custom post types in WordPress: • http://justintadlock.com/archives/2010/04/29/custom-post-types-in- wordpress • Blog post: First impressions of custom post type: • http://wpengineer.com/impressions-of-custom-post-type/ • Plugin: Custom post type UI plugin for WordPress • http://www.strangework.com/2010/03/03/custom-post-type-ui-plugin-for- wordpress/

    ×