stepping into custom post types
DESCRIPTION
TRANSCRIPT
![Page 1: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/1.jpg)
Stepping Into CustomPost Types
Photo by dolanh
K. Adam White, WordCamp Boston 2011
![Page 2: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/2.jpg)
Introduction
Who is this guy, anyway?
K.Adam White
Front-end developer & data architect
![Page 3: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/3.jpg)
Introduction
![Page 4: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/4.jpg)
Introduction
![Page 5: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/5.jpg)
Custom Post Types• What is a custom post type?
![Page 6: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/6.jpg)
Custom Post Types• What is a custom post type?
![Page 7: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/7.jpg)
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
![Page 8: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/8.jpg)
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’);
}
![Page 9: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/9.jpg)
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,
));
}
![Page 10: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/10.jpg)
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
![Page 11: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/11.jpg)
Default Template Fields• Title, Content, Publish box
![Page 12: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/12.jpg)
All Template Fields• Many more fields
available out of the box• Once enabled, can be
hidden or shown from the Screen Options tab
![Page 13: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/13.jpg)
Go Crazy!add_action('init', 'codex_custom_init');function codex_custom_init() {$labels = array('name' => _x('Books', 'post type general name'), 'singular_name' => _x('Book', 'post type singular name'),
'add_new' => _x('Add New', 'book'), 'add_new_item' => __('Add New Book'), 'edit_item' => __('Edit Book'), 'new_item' => __('New Book'), 'all_items' => __('All Books'), 'view_item' => __('View Book'), 'search_items' => __('Search Books'), 'not_found' => __('No books found'), 'not_found_in_trash' => __('No books found in Trash'),
'parent_item_colon' => '', 'menu_name' => 'Books‘
);$args = array('labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' =>
array('title','editor','author','thumbnail','excerpt','comments')); register_post_type('book',$args);
}
![Page 14: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/14.jpg)
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
![Page 15: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/15.jpg)
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
![Page 16: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/16.jpg)
Demo: Custom Post Type UI
![Page 17: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/17.jpg)
How to display your Post Types
• Template Hierarchy:
![Page 18: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/18.jpg)
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
![Page 19: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/19.jpg)
Using CPTs on other pagesFrom 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;
![Page 20: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/20.jpg)
Case Study: Nutfield Technology
![Page 21: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/21.jpg)
CPT #1: Home Page ItemsTitle, Content, Featured Image, and custom fields
![Page 22: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/22.jpg)
Custom Fields• Using WPAlchemy MetaBox• Allows for easier content maintenance
![Page 23: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/23.jpg)
CPT #2: Products• Content, Title,
Featured Image, File Attachments, 5 Taxonomies,3 WPAlchemycustom fields, Gravity Forms, and more
![Page 24: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/24.jpg)
Custom Taxonomies in Action
• Terms used as classes, to allow easy JavaScript filtering of a product list
![Page 25: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/25.jpg)
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
![Page 26: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/26.jpg)
Disadvantages• Adding custom fields is still code-heavy and
difficult• No visual ‘template builder’ in WordPress• Query syntax for custom posts can be tricky
![Page 27: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/27.jpg)
Gotcha’s1. 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.
![Page 28: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/28.jpg)
Gotcha’s2. 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)
![Page 29: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/29.jpg)
Gotcha’s3. 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.
![Page 30: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/30.jpg)
Gotcha’s4. 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.
![Page 31: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/31.jpg)
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
![Page 32: Stepping Into Custom Post Types](https://reader034.vdocuments.mx/reader034/viewer/2022042606/5455ff0fb1af9fb66e8b4c90/html5/thumbnails/32.jpg)
Thank You!• [email protected]• +K.Adam• @kadamwhite
• This is my first talk—Please rate me! http://speakerrate.com/talks/8024-stepping-into-custom-post-types