7. Custom Post Types
• What is a custom post type?
• Your own content type
• Blog Posts, Pages, Attachments, Revisions and
Menus are all “post types” in WordPress 3.0
8. Register your post type
• Created in functions.php (or a plugin) using
the “init” hook:
• add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type(‘nameOfCPT’);
}
9. A Basic Custom Post Type
• add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'acme_product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
)
);
}
10. Basic Post Type Attributes
• Names: How they display
in the admin screens (left)
• What fields the post type
supports: the content
editor, a title, thumbnails,
comments, etc
• Access rights
14. But I need more…
• You can use the build-in Custom Fields to
store custom data attributes
• You can extend the built-in template fields
with add_meta_box() or a related plugin: I
recommend WPAlchemy’s MetaBoxes
• Custom Taxonomies allow you create your
own tags and categories, just for a particular
Post type
15. But I’m not much of a coder…
• There’s a plugin for that
• Custom Post Type UI will let you create and
configure a post type without touching
functions.php
• Also supports custom taxonomies
18. Template Pages
• Create templates using the post type or
taxonomy’s name:
– single-{post_type}.php
– archive-{post_type}.php
– taxonomy-{taxonomy}.php
• e.g. if your post type is acme_products,
single-acme_product.php will be used when
you try to view a single product item
19. Using CPTs on other pages
From the WordPress Codex:
$args = array( 'post_type' => 'product',
'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
the_content();
endwhile;
23. CPT #2: Products
• Content, Title,
Featured Image,
File Attachments,
5 Taxonomies,
3 WPAlchemy
custom fields,
Gravity Forms,
and more
24. Custom Taxonomies in Action
• Terms used as classes, to allow easy JavaScript
filtering of a product list
25. Advantages of Custom Posts
• Modularization of content on the homepage
• Better Admin panel experience for editors
• Taxonomies allowed quick development of a
product selection application
• Allowed normal strengths of WordPress posts
(featured images, file attachments) to be used
on a custom template
26. Disadvantages
• Adding custom fields is still code-heavy and
difficult
• No visual ‘template builder’ in WordPress
• Query syntax for custom posts can be tricky
27. Gotcha’s
1. Namespaces:
Always prefix your post type names with a
namespace, e.g.
register_post_type(‘acme_products’);
instead of just ‘products.’ This will minimize
conflicts with other plugins.
28. Gotcha’s
2. Portability:
If you change your theme, you will need to
do a lot of work to make it compatible with
your custom post types
Locks you in to a particular theme (or theme
framework)
29. Gotcha’s
3. SEO:
To make the most of a custom post type, you
should re-write their URLs using the
‘rewrite’ => array(‘slug’ => ‘seo-friendly-title’)
parameter when creating the CPT. This will
also hide your namespace from the public.
30. Gotcha’s
4. Easy to get carried away:
Remember that another human will have to
maintain these items! Design for simplicity. A
more general data structure is usually the
best.
31. References
• Justin Tadlock’s Custom Post Types in WordPress
• WordPress Codex Custom Post Types page
• WPAlchemy custom MetaBox class
• Example add_meta_box() plugin on WP Engineer
• Custom Post Type UI plugin
32. Thank You!
• adam@kadamwhite.com
• +K.Adam
• @kadamwhite
• This is my first talk—Please rate me!
http://speakerrate.com/talks/8024-stepping-
into-custom-post-types