Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Ā
WordPress 3 Custom Post Types
1. Custom Post types
in WordPress 3
Dave Zille
May 15, 2010 - WordCamp Victoria BC
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. 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. WordPress 3: Not just for Blogs Anymore!
The term āblogā has been
replaced with āsiteā
throughout
WordPress 3 is (ofļ¬cially)
an actual CMS!
WordPress install process
now asks for āSite Titleā
7. Top 5 Reasons WP is a CMS:
1. Scalability
2. Security
8. Top 5 Reasons WP is a CMS:
1. Scalability
2. Security
3. Menu Management
9. Top 5 Reasons WP is a CMS:
1. Scalability
2. Security
3. Menu Management
4. Custom Taxonomies
10. Top 5 Reasons WP is a CMS:
1. Scalability
2. Security
3. Menu Management
4. Custom Taxonomies
5. Custom Post Types
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. 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. 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. 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. 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.
19. Your First Custom Post Type
ā¢ Case Study:
ā¢ Recipe database
ā¢ Description
20. Your First Custom Post Type
ā¢ Case Study:
ā¢ Recipe database
ā¢ Description
ā¢ Ingredients
21. Your First Custom Post Type
ā¢ Case Study:
ā¢ Recipe database
ā¢ Description
ā¢ Ingredients
ā¢ Prep Time
22. Your First Custom Post Type
ā¢ Case Study:
ā¢ Recipe database
ā¢ Description
ā¢ Ingredients
ā¢ Prep Time
ā¢ Cook Time
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,
)
);
}
25. Custom Post Types: The Code
ā¢ Where does the code go?
ā¢ 2 options:
ā¢ Create a plugin ļ¬le, or
ā¢ add to your themeās functions.php
26. Custom Post Types: The Code
ā¢ Where does the code go?
ā¢ 2 options:
ā¢ Create a plugin ļ¬le, 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..
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ā
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
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
35. Custom Post Types: The Code
register_post_type() arguments
ā¢ hierarchical
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. 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. 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. 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
ā¢ Speciļ¬es whether posts of the post type can be exportable using
WordPressā export function
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
ā¢ Speciļ¬es whether posts of the post type can be exportable using
WordPressā export function
ā¢ Default: true
44. Custom Post Types: The Code
register_post_type() arguments
ā¢ supports
ā¢ Deļ¬nes what meta boxes and other ļ¬elds appear when editing
or creating a post
45. Custom Post Types: The Code
register_post_type() arguments
ā¢ supports
ā¢ Deļ¬nes what meta boxes and other ļ¬elds appear when editing
or creating a post
ā¢ Options: title, editor, comments, trackbacks,
revisions, author, excerpt, thumbnail, custom-
ļ¬elds, page-attributes
46. Custom Post Types: The Code
register_post_type() arguments
ā¢ supports
ā¢ Deļ¬nes what meta boxes and other ļ¬elds appear when editing
or creating a post
ā¢ Options: title, editor, comments, trackbacks,
revisions, author, excerpt, thumbnail, custom-
ļ¬elds, page-attributes
ā¢ Default: title, editor
53. Custom Post Types: Displaying
ā¢ Customizing the custom post type
template
54. Custom Post Types: Displaying
ā¢ Customizing the custom post type
template
ā¢ single.php
55. Custom Post Types: Displaying
ā¢ Customizing the custom post type
template
ā¢ single.php
ā¢ template for the custom post type (default)
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. 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. 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
60. Custom Post Types: Displaying
ā¢ Displaying custom post types on your siteās
homepage
61. Custom Post Types: Displaying
ā¢ Displaying custom post types on your siteās
homepage
ā¢ Add to your themeās functions.php:
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. 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. 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. 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. 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. 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;
}
69. Other Custom Post Type Functions
ā¢ Get the āpost typeā of a post:
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
speciļ¬c post
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
speciļ¬c post
$post_type = get_post_type($post_id);
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
speciļ¬c post
$post_type = get_post_type($post_id);
ā¢ Check if a post is of a speciļ¬c type:
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
speciļ¬c post
$post_type = get_post_type($post_id);
ā¢ Check if a post is of a speciļ¬c type:
ā¢ is_post_type() allows you to check a speciļ¬c post against
a speciļ¬c post type
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
speciļ¬c post
$post_type = get_post_type($post_id);
ā¢ Check if a post is of a speciļ¬c type:
ā¢ is_post_type() allows you to check a speciļ¬c post against
a speciļ¬c post type
if ( is_post_type( 'recipe', $post_id ) )
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
speciļ¬c post
$post_type = get_post_type($post_id);
ā¢ Check if a post is of a speciļ¬c type:
ā¢ is_post_type() allows you to check a speciļ¬c post against
a speciļ¬c post type
if ( is_post_type( 'recipe', $post_id ) )
echo 'This is a not a blog post. It is a recipe.';
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
speciļ¬c post
$post_type = get_post_type($post_id);
ā¢ Check if a post is of a speciļ¬c type:
ā¢ is_post_type() allows you to check a speciļ¬c post against
a speciļ¬c post type
if ( is_post_type( 'recipe', $post_id ) )
echo 'This is a not a blog post. It is a recipe.';
else
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
speciļ¬c post
$post_type = get_post_type($post_id);
ā¢ Check if a post is of a speciļ¬c type:
ā¢ is_post_type() allows you to check a speciļ¬c post against
a speciļ¬c 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. 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/