Custom Post Types and Taxonomies

31
Custom Post Types & Taxonomies WordPress just got CMSier

description

WordPress just got CMSier

Transcript of Custom Post Types and Taxonomies

Page 1: Custom Post Types and Taxonomies

Custom Post Types & Taxonomies

WordPress just got CMSier

Page 2: Custom Post Types and Taxonomies

Who Am I?

I am a self-taught designer and programmer with over 5 years of experience working with a variety of businesses, non-profits, and individuals.

While I specialize in WordPress themes and CMS development, I also do all levels of Web Design and XHTML/CSS coding.

I freelance some through my personal brand, Tammy Hart Designs while also working a full time gig at blr | further as a UI Developer.

TammyHartDesigns.com

[email protected]

@tammyhart

Page 3: Custom Post Types and Taxonomies

WHAT IS A POST TYPE?Posts and pages and more! Oh my!

Page 4: Custom Post Types and Taxonomies

How Do They Work?

Should be called “Content Types”

Content goes into the posts table

Each content item is assigned a post_type

The queries output the content The default post type is

‘post’

Page 5: Custom Post Types and Taxonomies

Built in Types

Main Content Post – blog style content

Page – static content

Other Attachment – any images or other

files uploaded to a post (of any type)

Revision – an archive of versions of a particular post

Nav Menu Item – Used in the new menu system

Page 6: Custom Post Types and Taxonomies

Custom Types

Employees

Products

… any content that needs to be stored and used differently than blog posts or static pages

Won’t show up in main RSS feed

Page 7: Custom Post Types and Taxonomies

WHAT ISN’T A POST TYPE?Asides and galleries and links! Oh my!

Page 8: Custom Post Types and Taxonomies

Post Formats

Coming in WordPress 3.1

Think Tumblr, not post types

More like post’s post types, not different content types

Will show up in main RSS feed

Page 9: Custom Post Types and Taxonomies

More Information

Post Formats - WordPress.org Codexhttp://bit.ly/formats01

Post Formats vs. Custom Post Types – Mark Jaquithhttp://bit.ly/formats02

What, Whys, and How To’s of Post Formats in WordPress 3.1 – WP Beginnerhttp://bit.ly/formats03

Post types and formats and taxonomies, oh my! - Ottohttp://bit.ly/formats04

Page 10: Custom Post Types and Taxonomies

WHAT IS A TAXONOMY?Read my lips, now new taxonomies

Page 11: Custom Post Types and Taxonomies

How Do They Work?

Terms and their unique ID’s are saved in the terms table

Terms are assigned a taxonomy in the term_taxonomy table

Terms are related to posts (of all types) in the term_relationships table

Page 12: Custom Post Types and Taxonomies

Built in Taxonomies

Post Taxonomies Category

Post Tag

Other Link Category

Nav Menu

Page 13: Custom Post Types and Taxonomies

Custom Taxonomies

Employee departments

Product categories

… any way you want to tag or categorize your custom post types

Can be used with blog posts and static pages as well

Multiple taxonomy query coming in WordPress 3.1 Use Query Multiple Taxonomies

plugin

Page 14: Custom Post Types and Taxonomies

HAND CODINGThe hard way

Page 15: Custom Post Types and Taxonomies

Register a Post Typefunction post_type_movies() {

register_post_type( 'movies',array( 'label' => __('Movies'),

'public' => true, 'show_ui' => true,'supports' => array(

'post-thumbnails','excerpts','trackbacks','custom-fields','comments','revisions')

) );

register_taxonomy_for_object_type('post_tag', 'movies');}

add_action('init', 'post_type_movies');

First Impressions of Custom Post Type by Frank Bültge on WPEngineer.orghttp://bit.ly/posttypes

Page 16: Custom Post Types and Taxonomies

Default Arguments// Args prefixed with an underscore are reserved for internal use.$defaults = array( 'label' => false, 'publicly_queryable' => null, 'exclude_from_search' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array(), 'register_meta_box_cb' => null, 'taxonomies' => array(), 'show_ui' => null);

First Impressions of Custom Post Type by Frank Bültge on WPEngineer.orghttp://bit.ly/posttypes

Page 17: Custom Post Types and Taxonomies

The Taxonomy Codefunction post_type_movies() {

register_taxonomy( 'actor', 'movies', array(

'hierarchical' => true, 'label' => __('Actor')

) );

register_taxonomy( ‘director', 'movies',array(

'hierarchical' => false,'label' => __(‘Director'),'query_var' => ‘director','rewrite' => array('slug' => ‘director' )

));

}

add_action('init', 'post_type_movies');

First Impressions of Custom Post Type by Frank Bültge on WPEngineer.orghttp://bit.ly/posttypes

Page 18: Custom Post Types and Taxonomies

USING PLUGINSThe easy way

Page 19: Custom Post Types and Taxonomies

OverheardWordCamp Birmingham 2009

WordPress is the iPhone of the internet. You’ll always hear, “There’s a plugin for that!”

Page 20: Custom Post Types and Taxonomies

Plugins for Creating Them Custom Post Type UI

Creates both

TONS of labeling options

WP Post Type UI Creates both

Buggy

Allows a custom icon

More Types & More Taxonomies Allows you to override built in types and

taxonomies

Allows a custom icon

Works seamlessly with the More Fields plugin (stay tuned for more)

Page 21: Custom Post Types and Taxonomies

Plugins for Manipulating Them Custom Post Type Order

Drag and drop ordering

Simple Custom Post Type Archives Adds support for custom post type archive permalinks

Adds new body classes to custom post type archives

Adds a new conditional, is_custom_post_type_archive

Fixes the wp_title output on custom post type archives to show the custom type's label

Adds custom post type feeds

And more…

Post Type Switcher Change the post type one item at a time

Convert Post Types Bulk edit post types

Page 22: Custom Post Types and Taxonomies

CUSTOM TEMPLATESNow what do I do?

Page 23: Custom Post Types and Taxonomies

Template Hierarchy

Custom Post Type

single-{post_type}.php

single.php

index.php

Custom Post Types Display

Coming in version 3.1 Use Simple Custom Post Type

Archives plugin

Custom Taxonomy

taxonomy-{taxonomy}-{term}.php

taxonomy-{taxonomy}.php

taxonomy.php

archive.php

index.php

Page 24: Custom Post Types and Taxonomies

CUSTOM LOOPS & QUERIESThis gets hardcore

Page 25: Custom Post Types and Taxonomies

The LoopThe Basic Loop

<?

$loop = new WP_query('post_type=movies');

while ($loop->have_posts()) : $loop->the_post();

?>

... Do Stuff Here ...

<? endwhile; ?>

Add Custom Taxonomies

$loop = new WP_query('post_type=movies&actor=joaquin-phoenix');

$loop = new WP_query('post_type=movies&actor=joaquin-phoenix&director=m-night-shyamalan');

Page 26: Custom Post Types and Taxonomies

Term ListsGet All Terms

$terms = get_terms('actors', 'order_by=count&order=DESC');

Get Terms in a Post

get_the_term_list( $post->ID, 'actors', '<p>Actors: ', ',', '</p>' )

Other Functions

get_term get_term_children get_term_by get_taxonomies

Page 27: Custom Post Types and Taxonomies

MORE CUSTOM CONTENTBut wait, there’s more!

Page 28: Custom Post Types and Taxonomies

Matt MullenwegWordCamp Birmingham 2009

I like what you’re doing; I just don’t like the way you’re having to do it. So I’m going to make it better.

Page 29: Custom Post Types and Taxonomies

No More Flutter

Custom Post Types to create the special

content

Custom Taxonomies to organize the content

Custom Fields to get specific information

on each post in a way that is human and pretty

Page 30: Custom Post Types and Taxonomies

Customized Custom Fields

Plugins

More Fields Builds pretty boxes with custom

types of custom fields

Works perfectly with More Types and More Taxonomies

Custom fields Widget-like interface

Doesn’t seem to interact with custom post types

Easy Custom Fields Just as much coding as the hand

coded method

… not so easy

Hand Coded

add_meta_box() Basically limitless

Lots o’ code

WordPress.org Codexhttp://bit.ly/metabox01

Tutorial at Deluxe Blog Tipshttp://bit.ly/metabox02

We Function Tutorialhttp://bit.ly/metabox03

Page 31: Custom Post Types and Taxonomies

Thanks!

Questions?

TammyHartDesigns.com

[email protected]

@tammyhart