Stepping Into Custom Post Types

7,538 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,538
On SlideShare
0
From Embeds
0
Number of Embeds
1,937
Actions
Shares
0
Downloads
50
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Stepping Into Custom Post Types

  1. 1. K. Adam White, WordCamp Boston 2011 Stepping Into Custom Post TypesPhoto by dolanh
  2. 2. Introduction Who is this guy, anyway? K.Adam White Front-end developer & data architect
  3. 3. Introduction
  4. 4. Introduction
  5. 5. Custom Post Types• What is a custom post type?
  6. 6. Custom Post Types• What is a custom post type?
  7. 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. 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. 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. 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
  11. 11. Default Template Fields• Title, Content, Publish box
  12. 12. All Template Fields • Many more fields available out of the box • Once enabled, can be hidden or shown from the Screen Options tab
  13. 13. 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);}
  14. 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. 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
  16. 16. Demo: Custom Post Type UI
  17. 17. How to display your Post Types• Template Hierarchy:
  18. 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. 19. 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;
  20. 20. Case Study: Nutfield Technology
  21. 21. CPT #1: Home Page ItemsTitle, Content, Featured Image, and custom fields
  22. 22. Custom Fields• Using WPAlchemy MetaBox• Allows for easier content maintenance
  23. 23. CPT #2: Products • Content, Title, Featured Image, File Attachments, 5 Taxonomies, 3 WPAlchemy custom fields, Gravity Forms, and more
  24. 24. Custom Taxonomies in Action• Terms used as classes, to allow easy JavaScript filtering of a product list
  25. 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. 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. 27. 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.
  28. 28. 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)
  29. 29. 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.
  30. 30. 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.
  31. 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. 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

×