68
Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies Brad Williams WebDevStudios.com @williamsba

Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Embed Size (px)

DESCRIPTION

Brad Williams WordCamp Raleigh 2011 presentation on custom post types and taxonomies at WordCamp Raleigh.

Citation preview

Page 1: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Brad WilliamsWebDevStudios.com@williamsba

Page 2: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Brad WilliamsCo-Founder of WebDevStudios.com

Co-Host SitePoint Podcast

Co-Author of Professional WordPress (http://bit.ly/pro-wp)

& Professional WordPress Plugin Development (http://amzn.to/plugindevbook)

Who Am I?

Page 3: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

How to Kill Zombies

Topics

Page 4: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

How to Kill Zombies Explain Custom Post Types and Taxonomies Create Custom Post Types Create Custom Taxonomies Hooking into WordPress

Topics

Page 5: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

So What Are So What Are Custom Post Types?Custom Post Types?

Page 6: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

So What Are So What Are Custom Post Types?Custom Post Types?

Post type refers to the various structured data that is maintained in the WordPress posts table.

Page 7: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Huh?Huh?

Page 8: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

So What Are So What Are Custom Post Types Really?Custom Post Types Really?

ENGLISH: Custom Post Types allow you to create different types of content in WordPress.

Page 9: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Default WordPress Post Types:

• Posts• Pages• Attachments• Revisions• Nav Menus

Page 10: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Custom Post Type IdeasCustom Post Type Ideas• Podcasts• Movies• Bars• Forum• Quotes• Videos• Cars• House Listings• Events• Ticket System• etc, etc, etc

The possibilities are endless!

Page 11: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

To defeat your enemy you To defeat your enemy you must know your enemymust know your enemy

Tweet: @williamsba CAUTION ZOMBIES AHEAD! #wcraleigh

Win a copy of Professional WordPress!

Page 12: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

To defeat your enemy you To defeat your enemy you must know your enemymust know your enemy

Type: Nerd Zombies

Speed: Slow

Favorite Body Part: Braaaaaains

Page 13: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

To defeat your enemy you To defeat your enemy you must know your enemymust know your enemy

Type: Nazi Zombies

Speed: Fast

Favorite Part: Buttocks

Page 14: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

To defeat your enemy you To defeat your enemy you must know your enemymust know your enemy

Type: Baby Zombies

Speed: Crawl

Favorite Part: Fingers

Page 15: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

To defeat your enemy you To defeat your enemy you must know your enemymust know your enemy

Type: Kitty Zombies

Speed: Lightning Fast

Favorite Part: Fingers

Page 16: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Example:Example:

<?phpadd_action( 'init', 'wc_raleigh_cpt_init' );

function wc_raleigh_cpt_init() {

register_post_type( 'zombies' );

}?>

Drop the below code in your themes functions.php file

Page 17: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Questions?Questions?

That’s it!That’s it!

Page 18: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Example:Example:

Drop the below code in your themes functions.php file

Where is it?

Page 19: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Example:Example:

<?phpadd_action( 'init', 'wc_raleigh_cpt_init' );

function wc_raleigh_cpt_init() {

$arrrrgs = array('public' => true,'label' => 'Zombies'

);

register_post_type( 'zombies', $arrrrgs );

}?>

Set the public and label parameters for the custom post type

Page 20: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Example:Example:

It’s Alive!

Our new “Zombies” post type is now visible in the WP dashboard!

Page 21: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Example:Example:

<?phpadd_action( 'init', 'wc_raleigh_cpt_init' );

function wc_raleigh_cpt_init() {

$arrrrgs = array('public' => true,'label' => 'Zombies'

);

register_post_type( 'zombies', $arrrrgs );

}?>

Lets break it down:

1. Action hook to trigger our function

2. Execute the register_post_type function defining zombies as our custom post type

3. Set the label to Zombies

4. Set public to true. By default public is false which will hide your post type

Page 22: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: labelslabels

Page 23: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: labelslabels

An array of strings that represent your post type in the WP admin

name: The plural form of the name of your post type.singular_name: The singular form of the name of your post type.add_new: The menu item for adding a new post.add_new_item: The header shown when creating a new post.edit_item: The header shown when editing a post.new_item: Shown in the favorites menu in the admin header.view_item: Shown alongside the permalink on the edit post screen.search_items: Button text for the search box on the edit posts screen.not_found: Text to display when no posts are found through search in the admin.not_found_in_trash: Text to display when no posts are in the trash.menu_name – Text used in the WP dashboard menu

Ref: http://codex.wordpress.org/Function_Reference/register_post_type#Parameters

Page 24: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: labelslabels

add_action( 'init', 'wc_raleigh_cpt_init' );

function wc_raleigh_cpt_init() {

$labels = array('name' => 'Zombies','singular_name' => 'Zombie','add_new' => 'Add New Zombie','add_new_item' => 'Add New Zombie','edit_item' => 'Edit Zombie','new_item' => 'New Zombie','view_item' => 'View Zombie','search_items' => 'Search Zombies','not_found' => 'No zombies found','not_found_in_trash' => 'No zombies found in Trash','menu_name' => 'Zombies'

);

$arrrrgs = array('public' => true,'labels' => $labels

);

register_post_type( 'zombies', $arrrrgs );

}

An array of strings that represent your post type in the WP admin

Page 25: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: labelslabels

The Result

Page 26: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: supportssupports

Defines what meta boxes and other fields appear on the edit screen

title: Text input field for the post title.editor: Main content meta boxcomments: Ability to turn comments on/off.trackbacks: Ability to turn trackbacks and pingbacks on/off.revisions: Allows revisions to be made of your post.author: Displays the post author select box.excerpt: A textarea for writing a custom excerpt.thumbnail: The post thumbnail (featured imaged) upload box.custom-fields: Custom fields input area.page-attributes: The attributes box shown for pages. Important for hierarchical post types, so you can select the parent post.post-formats: Add post formats

Page 27: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: supportssupports

Defines what meta boxes and other fields appear on the edit screen

register_post_type('zombies', array(

'labels' => array('name' => 'Zombies','singular_name' => 'Zombie','add_new' => 'Add New Zombie','add_new_item' => 'Add New Zombie','edit' => 'Edit Zombies','edit_item' => 'Edit Zombie','new_item' => 'New Zombie','view' => 'View Zombie','view_item' => 'View Zombie','search_items' => 'Search Zombies','not_found' => 'No zombies found','not_found_in_trash' => 'No zombies found in Trash','parent' => 'Parent Zombie',

),'supports' => array('title'),'public' => true,)

);

Page 28: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: supportssupports

Defines what meta boxes and other fields appear on the edit screen

Only the Title and Publish meta boxes are displayed

Page 29: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: supportssupports

Now lets activate all meta boxes

$labels = array('name' => 'Zombies','singular_name' => 'Zombie','add_new' => 'Add New Zombie','add_new_item' => 'Add New Zombie','edit_item' => 'Edit Zombie','new_item' => 'New Zombie','view_item' => 'View Zombie','search_items' => 'Search Zombies','not_found' => 'No zombies found','not_found_in_trash' => 'No zombies found in Trash','menu_name' => 'Zombies'

);

$arrrrgs = array('public' => true,'labels' => $labels,'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks',

'custom-fields', 'comments', 'revisions', 'page-attributes', 'post-formats' ) );

register_post_type( 'zombies', $arrrrgs );

Page 30: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: supportssupports

Now all meta boxes are displayed on the Zombie edit screen

Page 31: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: rewriterewrite

Defines the permalink structure of your custom post type posts

slug: The slug to use in your permalink structure

'rewrite' => array('slug' => 'enemy')Example:

BEFORE

AFTER

TIP: If you receive a 404 after setting a custom rewrite visit Settings > Permalinks and save your permalink settings to flush the rewrite rules in WordPress

Page 32: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: taxonomiestaxonomies

Add pre-existing taxonomies to your custom post type

'taxonomies' => array( 'post_tag', 'category')

Example:

Page 33: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: miscmisc

Below are additional arguments for creating custom post types

hierarchical: whether the post type is hierarchicaldescription: a text description of your custom post typeshow_ui: whether to show the admin menus/screensmenu_position: Set the position of the menu order where the post type should appearmenu_icon: URL to the menu icon to be usedexclude_from_search: whether post type content should appear in search resultscan_export: Can this post type be exportedshow_in_menu: Whether to show the post type in an existing admin menuhas_archive: Enables post type archives

Page 34: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: show_in_menushow_in_menu

Add the custom post type to an existing menu

BEFORE AFTER

'show_in_menu' => 'edit.php?post_type=page'

Page 35: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: has_archivehas_archive

Creates a default page to list all entries in a post type

has_archive' => ‘enemies'

http://example.com/enemies/

Now lists zombie posts

Can also create a theme template named archive-zombies.php

Page 36: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Putting it all togetherPutting it all togetheradd_action( 'init', 'wc_raleigh_cpt_init' );

function wc_raleigh_cpt_init() {

$labels = array('name' => 'Zombies','singular_name' => 'Zombie','add_new' => 'Add New Zombie','add_new_item' => 'Add New Zombie','edit_item' => 'Edit Zombie','new_item' => 'New Zombie','view_item' => 'View Zombie','search_items' => 'Search Zombies','not_found' => 'No zombies found','not_found_in_trash' => 'No zombies found in Trash','menu_name' => 'Zombies'

);

$arrrrgs = array('public' => true,'labels' => $labels,'rewrite' => array('slug' => 'enemy'),'taxonomies' => array( 'post_tag', 'category'),'show_in_menu' => 'edit.php?post_type=page','has_archive' => 'enemies','supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields',

'comments', 'revisions', 'page-attributes', 'post-formats' ) );

register_post_type( 'zombies', $arrrrgs );

}

Page 37: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Now that we know the enemyNow that we know the enemywe must kill the enemywe must kill the enemy

Page 38: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Zombie WeaponsZombie Weapons

Type: Machete

Nickname: Butter Cutter

Gore Factor: Moderate

Kill Speed: Swift

Page 39: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Zombie WeaponsZombie Weapons

Type: Spiked Bat

Nickname: Pokie

Gore Factor: Gore-cano

Kill Speed: Slow

Page 40: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Zombie WeaponsZombie Weapons

Type: Chainsaw

Nickname: Old Faithful

Gore Factor: Over the Top

Kill Speed: Slow and Steady

Page 41: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Zombie WeaponsZombie Weapons

Type: All in One Zombie Killer

Nickname: The Torbert

Gore Factor: Messy or Clean. You Choose!

Kill Speed: Instant

Page 42: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Create another custom post type for Weapons

$labels = array('name' => 'Weapons','singular_name' => 'Weapon','add_new' => 'Add New Weapon','add_new_item' => 'Add New Weapon','edit_item' => 'Edit Weapon','new_item' => 'New Weapon','view_item' => 'View Weapon','search_items' => 'Search Weapons','not_found' => 'No weapons found','not_found_in_trash' => 'No weapons found in Trash','menu_name' => 'Weapons'

);

$arrrrgs = array('public' => true,'labels' => $labels,'rewrite' => array('slug' => 'weapon'),'has_archive' => 'weapons','supports' => array( 'title', 'editor', 'thumbnail' )

);

register_post_type( 'weapons', $arrrrgs );

Weapons Custom Post TypeWeapons Custom Post Type

Page 43: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

So what are Taxonomies?So what are Taxonomies?

ENGLISH: Taxonomies are a way to group similar items together

Page 44: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Default WordPress Taxonomies:

• Category• Tag• Link Category

Page 45: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Example Time!Example Time!

Tweet: @williamsba ZOMBIES IN AREA! RUN #wcraleigh

Win a copy of Professional WordPress!

Page 46: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Example:Example:

<?php $arrrrgs = array(

'label' => ‘Speed' );

register_taxonomy( ‘speed', 'zombies', $arrrrgs );?>

Drop the below code in your themes functions.php file

Non-hierarchical Taxonomy

Page 47: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Example:Example:

New custom taxonomy is automatically added to the Zombies post type menu

The Speed meta box is also automatically added to the

Zombie edit screen!

Page 48: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: hierarchicalhierarchical

Give the custom taxonomy hierarchy

BEFORE AFTER

‘hierarchical' => true

Page 49: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: labelsAdditional Arguments: labels

An array of strings that represent your taxonomy in the WP admin

name: The general name of your taxonomysingular_name: The singular form of the name of your taxonomy.search_items: The search items textpopular_items: The popular items textall_items: The all items textparent_item: The parent item text. Only used on hierarchical taxonomiesparent_item_colon: Same as parent_item, but with a colonedit_item: The edit item textupdate_item: The update item textadd_new_item: The add new item textnew_item_name: The new item name textseparate_items_with_commas: The separate items with commas textadd_or_remove_items: The add or remove items textchoose_from_most_used: The choose from most used textmenu_name: The string used for the menu name

Page 50: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: labelsAdditional Arguments: labels

$labels = array('name' => 'Speed','singular_name' => 'Speed','search_items' => 'Search Speeds','all_items' => 'All Speeds','parent_item' => 'Parent Speed','parent_item_colon' => 'Parent Speed:','edit_item' => 'Edit Speed','update_item' => 'Update Speed','add_new_item' => 'Add New Speed','new_item_name' => 'New Genre Speed','menu_name' => 'Speed'

);

$arrrrgs = array('hierarchical' => false,'labels' => $labels

);

register_taxonomy( 'speed', 'zombies', $arrrrgs );

An array of strings that represent your taxonomy in the WP admin

Page 51: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: labelsAdditional Arguments: labels

The Result

Page 52: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: Additional Arguments: rewriterewrite

Defines the permalink structure for your custom taxonomy

slug: The slug to use in your permalink structure

'rewrite' => array( 'slug' => ‘quickness' )Example:

BEFORE

AFTER

http://example.com/speed/ripe/

http://example.com/quickness/ripe/

Page 53: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Additional Arguments: miscAdditional Arguments: misc

Below are additional arguments for creating custom taxonomies

public: whether the taxonomy is publicly queryableshow_ui: whether to display the admin UI for the taxonomyquery_var: whether to be able to query posts using the taxonomy. show_tagcloud: whether to show a tag cloud in the admin UIshow_in_nav_menus: whether the taxonomy is available for selection in nav menus

Page 54: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Putting it all togetherPutting it all together

$labels = array('name' => 'Speed','singular_name' => 'Speed','search_items' => 'Search Speeds','all_items' => 'All Speeds','parent_item' => 'Parent Speed','parent_item_colon' => 'Parent Speed:','edit_item' => 'Edit Speed','update_item' => 'Update Speed','add_new_item' => 'Add New Speed','new_item_name' => 'New Genre Speed','menu_name' => 'Speed'

);

$arrrrgs = array('hierarchical' => false,'labels' => $labels

);

register_taxonomy( 'speed', 'zombies', $arrrrgs );

Page 55: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies
Page 56: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Displaying in WordPress

#protip

Page 57: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Displaying Custom Post Type Content

<?php query_posts( array( 'post_type' => 'zombies' ) ); ?>

By default custom post type content will NOT display in the Loop

Placing the above code directly before the Loop will only display our zombies

Page 58: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Displaying Custom Post Type Content

<?php query_posts( array( 'post_type' => 'zombies' ) ); ?>

BEFORE ZOMBIED(After)

Page 59: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Custom Loop

<?php $zombies = new WP_Query( array( 'post_type' => 'zombies', 'posts_per_page' => 1, 'orderby' => 'rand' ) );

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

the_title( '<h2><a href="' . get_permalink() . '" >', '</a></h2>' );?><div class="entry-content">

<?php the_content(); ?></div>

<?php endwhile; ?>

Creating a custom Loop for your post type

Page 60: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

<?php if (have_posts()) : while (have_posts()) : the_post();

$post_type = get_post_type( get_the_ID() ); if ($post_type == 'zombies') {

echo 'This is a Zombie!'; }?>

<?php endwhile; ?><?php endif; ?>

Function: get_post_type()

Returns the post type of the content you are viewing

Custom Post Type Functions

http://codex.wordpress.org/Function_Reference/get_post_type

Page 61: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

if ( post_type_exists( 'zombies' ) ) {echo 'Zombies Exist!';

}

Function: post_type_exists()

Check if a post type exists

Custom Post Type Functions

$post_types = get_post_types( '','names' );foreach ( $post_types as $post_type ) { echo '<p>'. $post_type. '</p>';}

Function: get_post_types()

List all registered post types in WordPress

Page 62: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

<?php echo get_the_term_list( $post->ID, speed', ‘Speed: ', ', ', '' ); ?>

Function: get_the_term_list()

Display speed taxonomy for our zombies

Custom Taxonomy Functions

http://codex.wordpress.org/Function_Reference/get_the_term_list

Page 63: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Recommended Plugin

Page 64: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Custom Post Type UI

http://wordpress.org/extend/plugins/custom-post-type-ui/

Easily create custom post types without writing code!

Page 65: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Custom Post Type UI

http://wordpress.org/extend/plugins/custom-post-type-ui/

Also easily create custom taxonomies without writing code!

Page 66: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Custom Post Type UI

http://wordpress.org/extend/plugins/custom-post-type-ui/

Plugin also gives you the PHP code for custom post types and taxonomies!

Page 67: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Related Codex Articles› http://codex.wordpress.org/Post_Types› http://codex.wordpress.org/Function_Reference/register_post_type› http://codex.wordpress.org/Taxonomies

Custom Post Type Articles› http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress› http://justintadlock.com/archives/2010/02/02/showing-custom-post-types-on-your-home-

blog-page› http://kovshenin.com/archives/custom-post-types-in-wordpress-3-0/› http://wpengineer.com/impressions-of-custom-post-type/› http://kovshenin.com/archives/custom-post-types-in-wordpress-3-0/

Custom Taxonomies› http://justintadlock.com/archives/2009/05/06/custom-taxonomies-in-wordpress-28› http://justintadlock.com/archives/2009/06/04/using-custom-taxonomies-to-create-a-movie-

database› http://www.shibashake.com/wordpress-theme/wordpress-custom-taxonomy-input-panels

Custom Post Type and Taxonomy Resources

Page 68: Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies

Brad [email protected]

Blog: strangework.com

Twitter: @williamsba

IRC: WDS-Brad

Contact

http://bit.ly/pro-wphttp://amzn.to/plugindevbook