37
DEMYSTIFYING CUSTOM POST TYPES & TAXONOMIES Tracey Kemp @dogshindleg

Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

Embed Size (px)

DESCRIPTION

Tracey Kemp takes us through the use of Custom Post Types and Taxonomies in Wordpress 3+ #wcsyd

Citation preview

Page 1: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

DEMYSTIFYING CUSTOM POST TYPES & TAXONOMIESTracey Kemp@dogshindleg

Page 2: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

➡ Registering Custom Post Types and Taxonomies

➡ A plugin that can help

➡ Displaying content from custom post types and taxonomies

➡ Resources

Page 3: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

A record label site with artists and their releases.

FINAL RESULT

Page 4: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

Custom Post Type Taxonomy

Artist Location

The Drones VIC

The Kill Devil Hills WA

Midget NSW / QLD

Front End Loader NSW

Dan Kelly VIC

Custom Post Type Taxonomy Taxonomy

Release Artist Year

Here Come The Lies The Drones 2002

Wait Long By The River The Drones 2005

Gala Mill The Drones 2006

Havilah The Drones 2008

Heathen Songs The Kill Devil Hills 2004

The Drought The Kill Devil Hills 2006

Man, You Should Explode The Kill Devil Hills 2010

Vagus Wandering Midget 1994

The Toggle Switch Midget 1997

Total Abandonment Of Better Understanding

Midget 1998

Let's Ride Front End Loader 1995

What will be a custom post type and what will be a taxonomy?

PLANNING

Page 5: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

REGISTER CUSTOM POST TYPES

Page 6: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

function register_custom_post_types() {

}

add_action(‘init’, ‘register_custom_post_types’);

// register post types hereREGISTER CUSTOM POST TYPES

Page 7: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

REGISTER ARTISTS

function register_custom_post_types() {

}

add_action(‘init’, ‘register_custom_post_types’);

register_post_type( ‘artists’, array( ‘labels’ => array( ‘name’ => _x(‘Artists’, ‘post type general name’), ‘singular_name’ => __( ‘Artist’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Artists’ ), ‘edit_item’ => __( ‘Edit Artist’ ), ‘add_new_item’ => __( ‘Add New Artist’ ), ‘new_item_name’ => __( ‘New Artist’ ), ), ‘public’ => true, ‘show_ui’ => true, ‘hierarchical’ => true, ‘supports’ => array (‘title’,‘editor’, ‘thumbnail’) ) );

Page 8: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

REGISTER ARTISTS

REGISTER RELEASES

function register_custom_post_types() {

}

add_action(‘init’, ‘register_custom_post_types’);

register_post_type( ‘artists’, array( ‘labels’ => array( ‘name’ => _x(‘Artists’, ‘post type general name’), ‘singular_name’ => __( ‘Artist’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Artists’ ), ‘edit_item’ => __( ‘Edit Artist’ ), ‘add_new_item’ => __( ‘Add New Artist’ ), ‘new_item_name’ => __( ‘New Artist’ ), ), ‘public’ => true, ‘show_ui’ => true, ‘hierarchical’ => true, ‘supports’ => array (‘title’,‘editor’, ‘thumbnail’) ) );

Page 9: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

register_post_type( ‘releases’, array( ‘labels’ => array( ‘name’ => _x(‘Releases’, ‘post type general name’), ‘singular_name’ => __( ‘Release’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Releases’ ), ‘edit_item’ => __( ‘Edit Release’ ), ‘add_new_item’ => __( ‘Add New Release’ ), ‘new_item_name’ => __( ‘New Release’ ), ), ‘public’ => true, ‘show_ui’ => true, ‘hierarchical’ => true, ‘supports’ => array (‘title’, ‘editor’, ‘thumbnail’) ) );

REGISTER ARTISTS

REGISTER RELEASES

function register_custom_post_types() {

}

add_action(‘init’, ‘register_custom_post_types’);

register_post_type( ‘artists’, array( ‘labels’ => array( ‘name’ => _x(‘Artists’, ‘post type general name’), ‘singular_name’ => __( ‘Artist’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Artists’ ), ‘edit_item’ => __( ‘Edit Artist’ ), ‘add_new_item’ => __( ‘Add New Artist’ ), ‘new_item_name’ => __( ‘New Artist’ ), ), ‘public’ => true, ‘show_ui’ => true, ‘hierarchical’ => true, ‘supports’ => array (‘title’,‘editor’, ‘thumbnail’) ) );

Page 10: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

register_post_type( ‘releases’, array( ‘labels’ => array( ‘name’ => _x(‘Releases’, ‘post type general name’), ‘singular_name’ => __( ‘Release’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Releases’ ), ‘edit_item’ => __( ‘Edit Release’ ), ‘add_new_item’ => __( ‘Add New Release’ ), ‘new_item_name’ => __( ‘New Release’ ), ), ‘public’ => true, ‘show_ui’ => true, ‘hierarchical’ => true, ‘supports’ => array (‘title’, ‘editor’, ‘thumbnail’) ) );

REGISTER ARTISTS

REGISTER RELEASES

function register_custom_post_types() {

}

add_action(‘init’, ‘register_custom_post_types’);

register_post_type( ‘artists’, array( ‘labels’ => array( ‘name’ => _x(‘Artists’, ‘post type general name’), ‘singular_name’ => __( ‘Artist’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Artists’ ), ‘edit_item’ => __( ‘Edit Artist’ ), ‘add_new_item’ => __( ‘Add New Artist’ ), ‘new_item_name’ => __( ‘New Artist’ ), ), ‘public’ => true, ‘show_ui’ => true, ‘hierarchical’ => true, ‘supports’ => array (‘title’,‘editor’, ‘thumbnail’) ) );

Page 11: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

REGISTER TAXONOMIES

Page 12: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

// register post types hereREGISTER TAXONOMIES

function register_taxonomies() {

}

add_action(‘init’, ‘register_taxonomies’, 0);

Page 13: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

$labels = array( ‘name’ => _x( ‘Location’, ‘taxonomy general name’ ), ‘singular_name’ => _x( ‘Location’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Locations’ ), ‘edit_item’ => __( ‘Edit Location’ ), ‘add_new_item’ => __( ‘Add New Location’ ), ‘menu_name’ => __( ‘Locations’ ), ); register_taxonomy( ‘locations’, array(‘artists’), // post-type(s) array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) );

REGISTER LOCATION

}

add_action(‘init’, ‘register_taxonomies’, 0);

function register_taxonomies() {

Page 14: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

REGISTER LOCATION

REGISTER RELEASE YEAR

function register_taxonomies() {

$labels = array( ‘name’ => _x( ‘Location’, ‘taxonomy general name’ ), ‘singular_name’ => _x( ‘Location’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Locations’ ), ‘edit_item’ => __( ‘Edit Location’ ), ‘add_new_item’ => __( ‘Add New Location’ ), ‘menu_name’ => __( ‘Locations’ ), ); register_taxonomy( ‘locations’, array(‘artists’), // post-type(s) array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) );

}

add_action(‘init’, ‘register_taxonomies’, 0);

Page 15: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

$labels = array( ‘name’ => _x( ‘Release Year’, ‘taxonomy general name’ ), ‘singular_name’ => _x( ‘Release Year’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Release Years’ ), ‘edit_item’ => __( ‘Edit Release Year’ ), ‘add_new_item’ => __( ‘Add New Release Year’ ), ‘menu_name’ => __( ‘Release Years’ ), ); register_taxonomy( ‘release-year’, array(‘releases’), // post-type(s) array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) );

REGISTER LOCATION

REGISTER RELEASE YEAR

function register_taxonomies() {

$labels = array( ‘name’ => _x( ‘Location’, ‘taxonomy general name’ ), ‘singular_name’ => _x( ‘Location’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Locations’ ), ‘edit_item’ => __( ‘Edit Location’ ), ‘add_new_item’ => __( ‘Add New Location’ ), ‘menu_name’ => __( ‘Locations’ ), ); register_taxonomy( ‘locations’, array(‘artists’), // post-type(s) array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) );

}

add_action(‘init’, ‘register_taxonomies’, 0);

Page 16: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

$labels = array( ‘name’ => _x( ‘Release Year’, ‘taxonomy general name’ ), ‘singular_name’ => _x( ‘Release Year’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Release Years’ ), ‘edit_item’ => __( ‘Edit Release Year’ ), ‘add_new_item’ => __( ‘Add New Release Year’ ), ‘menu_name’ => __( ‘Release Years’ ), ); register_taxonomy( ‘release-year’, array(‘releases’), // post-type(s) array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) );

REGISTER LOCATION

REGISTER RELEASE YEAR

function register_taxonomies() {

$labels = array( ‘name’ => _x( ‘Location’, ‘taxonomy general name’ ), ‘singular_name’ => _x( ‘Location’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Locations’ ), ‘edit_item’ => __( ‘Edit Location’ ), ‘add_new_item’ => __( ‘Add New Location’ ), ‘menu_name’ => __( ‘Locations’ ), ); register_taxonomy( ‘locations’, array(‘artists’), // post-type(s) array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) );

}

add_action(‘init’, ‘register_taxonomies’, 0);

Page 17: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

USING A CUSTOM POST TYPE AS A TAXONOMY

Page 18: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012
Page 19: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012
Page 20: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012
Page 21: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

DISPLAY ARTISTS

Page 22: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

<?php //artists posts $artist_args=array( 'post_type' => 'artists', 'posts_per_page' => -1, ); $artist_query = null; $artist_query = new WP_Query($artist_args); if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2> <?php the_excerpt(); ?> </article> <?php endwhile; } wp_reset_query(); ?>

DISPLAY ARTISTS

Page 23: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

<?php //artists posts $artist_args=array( 'post_type' => 'artists', 'posts_per_page' => -1, ); $artist_query = null; $artist_query = new WP_Query($artist_args); if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2>

<?php the_excerpt(); ?> </article> <?php endwhile; } wp_reset_query(); ?>

<?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?>

DISPLAY ARTISTS WITH LOCATIONS

Page 24: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

DISPLAY ARTISTS WITH LOCATIONS

<?php //artists posts $artist_args=array( 'post_type' => 'artists', 'posts_per_page' => -1, ); $artist_query = null; $artist_query = new WP_Query($artist_args); if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2>

<?php the_excerpt(); ?> </article> <?php endwhile; } wp_reset_query(); ?>

<?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?>

<?php echo get_the_term_list( $id, $taxonomy, $before, $sep, $after ) ?>

<?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?>

Page 25: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

<?php //releases posts $release_args=array( 'post_type' => 'releases', 'posts_per_page' => -1, ); $release_query = null; $release_query = new WP_Query($release_args); if( $release_query->have_posts() ) { while ($release_query->have_posts()) : $release_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php echo get_the_term_list( $post->ID, ‘release-year’, ‘(’, ‘‘, ‘)’ ); ?> </article> <?php endwhile; } wp_reset_query(); ?>

DISPLAY RELEASES

Page 26: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

<?php //releases posts $release_args=array( 'post_type' => 'releases', 'posts_per_page' => -1, ); $release_query = null; $release_query = new WP_Query($release_args); if( $release_query->have_posts() ) { while ($release_query->have_posts()) : $release_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php echo get_the_term_list( $post->ID, ‘release-year’, ‘(’, ‘‘, ‘)’ ); ?> </article> <?php endwhile; } wp_reset_query(); ?>

DISPLAY RELEASES

Page 27: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

HOW DO WE SHOW ARTISTS WITH RELEASES?

Page 28: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

<?php //artists posts $artist_args=array( 'post_type' => 'artists', 'posts_per_page' => -1, ); $artist_query = null; $artist_query = new WP_Query($artist_args); if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2> <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?>

//insert releases here

</article> <?php endwhile; } wp_reset_query(); ?>

DISPLAY ARTISTS

Page 29: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2> <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?>

</article>

<?php //releases posts $release_args=array( 'post_type' => 'releases', 'posts_per_page' => -1, ); $release_query = null; $release_query = new WP_Query($release_args); if( $release_query->have_posts() ) { while ($release_query->have_posts()) : $release_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php echo get_the_term_list( $post->ID, ‘release-year’, ‘(’, ‘‘, ‘)’ ); ?> </article> <?php endwhile; } wp_reset_query(); ?>

DISPLAY ARTISTS

DISPLAY RELEASES

Page 30: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2> <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?>

<?php //releases posts $release_args=array( 'post_type' => 'releases', 'posts_per_page' => -1, ); $release_query = null; $release_query = new WP_Query($release_args); if( $release_query->have_posts() ) { while ($release_query->have_posts()) : $release_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php echo get_the_term_list( $post->ID, ‘release-year’, ‘(’, ‘‘, ‘)’ ); ?> </article> <?php endwhile; } wp_reset_query(); ?>

DISPLAY ARTISTS

DISPLAY RELEASES

GET ARTIST TITLE

Page 31: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2> <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?>

<?php $tax_artist = get_the_title(); ?>

<?php //releases posts $release_args=array( 'post_type' => 'releases', 'posts_per_page' => -1, ); $release_query = null; $release_query = new WP_Query($release_args); if( $release_query->have_posts() ) { while ($release_query->have_posts()) : $release_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php echo get_the_term_list( $post->ID, ‘release-year’, ‘(’, ‘‘, ‘)’ ); ?> </article> <?php endwhile; } wp_reset_query(); ?>

DISPLAY ARTISTS

DISPLAY RELEASES

GET ARTIST TITLE

Page 32: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2> <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?>

<?php $tax_artist = get_the_title(); ?>

'artists' => $tax_artist,

<?php //releases posts $release_args=array( 'post_type' => 'releases',

'posts_per_page' => -1, ); $release_query = null; $release_query = new WP_Query($release_args); if( $release_query->have_posts() ) { while ($release_query->have_posts()) : $release_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php echo get_the_term_list( $post->ID, ‘release-year’, ‘(’, ‘‘, ‘)’ ); ?> </article>

DISPLAY ARTISTS

DISPLAY RELEASES

$taxonomy => $term

GET ARTIST TITLE

Page 33: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

<?php //artists posts $artist_args=array( 'post_type' => 'artists', 'posts_per_page' => -1, ); $artist_query = null; $artist_query = new WP_Query($artist_args); if( $artist_query->have_posts() ) { while ($artist_query->have_posts()) : $artist_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <h2><?php the_title(); ?></h2> <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?>

<?php $tax_artist = get_the_title(); ?>

'artists' => $tax_artist,

<?php //releases posts $release_args=array( 'post_type' => 'releases',

'posts_per_page' => -1, ); $release_query = null; $release_query = new WP_Query($release_args); if( $release_query->have_posts() ) { while ($release_query->have_posts()) : $release_query->the_post(); ?> <article> <?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php echo get_the_term_list( $post->ID, ‘release-year’, ‘(’, ‘‘, ‘)’ ); ?> </article> <?php endwhile; } wp_reset_query(); ?>

</article> <?php endwhile; } wp_reset_query(); ?>

START ARTISTS QUERY

GET ARTIST TITLE

$taxonomy => $term

CLOSE ARTISTS

START RELEASES QUERY

CLOSE RELEASES

Page 34: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012
Page 35: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

RESOURCES

http://codex.wordpress.org/http://wordpress.org/support/

WordPress Codex & Forums

Register Custom Post Typeshttp://codex.wordpress.org/Function_Reference/register_post_type

Register Taxonomieshttp://codex.wordpress.org/Function_Reference/register_taxonomy

Query Postshttp://codex.wordpress.org/Function_Reference/query_posts

The Loophttp://codex.wordpress.org/The_Loop

Page 37: Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012

DEMYSTIFYING CUSTOM POST TYPES & TAXONOMIESTracey Kemp@dogshindleg

Full code samples and explanations

dogshindleg.com