Stepping Into Custom Post Types

Post on 01-Nov-2014

7.103 views 2 download

Tags:

description

 

Transcript of Stepping Into Custom Post Types

Stepping Into CustomPost Types

Photo by dolanh

K. Adam White, WordCamp Boston 2011

Introduction

Who is this guy, anyway?

K.Adam White

Front-end developer & data architect

Introduction

Introduction

Custom Post Types• What is a custom post type?

Custom Post Types• What is a custom post type?

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

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’);

}

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,

));

}

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

Default Template Fields• Title, Content, Publish box

All Template Fields• Many more fields

available out of the box• Once enabled, can be

hidden or shown from the Screen Options tab

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);

}

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

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

Demo: Custom Post Type UI

How to display your Post Types

• Template Hierarchy:

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

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;

Case Study: Nutfield Technology

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

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

CPT #2: Products• Content, Title,

Featured Image, File Attachments, 5 Taxonomies,3 WPAlchemycustom fields, Gravity Forms, and more

Custom Taxonomies in Action

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

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

Disadvantages• Adding custom fields is still code-heavy and

difficult• No visual ‘template builder’ in WordPress• Query syntax for custom posts can be tricky

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.

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)

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.

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.

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

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