32
Stepping Into Custom Post Types Photo by dolanh K. Adam White, WordCamp Boston 2011

Stepping Into Custom Post Types

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Stepping Into Custom Post Types

Stepping Into CustomPost Types

Photo by dolanh

K. Adam White, WordCamp Boston 2011

Page 2: Stepping Into Custom Post Types

Introduction

Who is this guy, anyway?

K.Adam White

Front-end developer & data architect

Page 3: Stepping Into Custom Post Types

Introduction

Page 4: Stepping Into Custom Post Types

Introduction

Page 5: Stepping Into Custom Post Types

Custom Post Types• What is a custom post type?

Page 6: Stepping Into Custom Post Types

Custom Post Types• What is a custom post type?

Page 7: Stepping Into Custom Post Types

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

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

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

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

Default Template Fields• Title, Content, Publish box

Page 12: Stepping Into Custom Post Types

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

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

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

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

Demo: Custom Post Type UI

Page 17: Stepping Into Custom Post Types

How to display your Post Types

• Template Hierarchy:

Page 18: Stepping Into Custom Post Types

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

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

Case Study: Nutfield Technology

Page 21: Stepping Into Custom Post Types

CPT #1: Home Page ItemsTitle, Content, Featured Image, and custom fields

Page 22: Stepping Into Custom Post Types

Custom Fields• Using WPAlchemy MetaBox• Allows for easier content maintenance

Page 23: Stepping Into Custom Post Types

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

Custom Taxonomies in Action

• Terms used as classes, to allow easy JavaScript filtering of a product list

Page 25: Stepping Into Custom Post Types

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

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

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

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

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

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

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