DEMYSTIFYINGCUSTOM POSTTYPES &TAXONOMIESTracey Kemp@dogshindleg
➡ Registering Custom Post Types  and Taxonomies➡ A plugin that can help➡ Displaying content from custom  post types and ta...
FINAL RESULTA record label site withartists and their releases.
PLANNINGWhat will be a custom post type andwhat will be a taxonomy?Custom Post Type       Taxonomy    Custom Post Type    ...
REGISTERCUSTOMPOSTTYPES
REGISTER   function register_custom_post_types() {                // register post types hereCUSTOM           }POST       ...
function register_custom_post_types() {                register_post_type(                      ‘artists’,                ...
function register_custom_post_types() {                  register_post_type(                          ‘artists’,          ...
function register_custom_post_types() {                  register_post_type(                          ‘artists’,          ...
function register_custom_post_types() {                  register_post_type(                          ‘artists’,          ...
REGISTERTAXONOMIES
REGISTER   function register_taxonomies() {TAXO            // register post types hereNOMIES     }           add_action(‘i...
function register_taxonomies() {                $labels = array(                   ‘name’ => _x( ‘Location’, ‘taxonomy gen...
function register_taxonomies() {                   $labels = array(                        ‘name’ => _x( ‘Location’, ‘taxo...
function register_taxonomies() {                   $labels = array(                        ‘name’ => _x( ‘Location’, ‘taxo...
function register_taxonomies() {                   $labels = array(                        ‘name’ => _x( ‘Location’, ‘taxo...
USING ACUSTOM POSTTYPE AS ATAXONOMY
DISPLAYARTISTS
DISPLAYARTISTS<?php //artists posts$artist_args=array(     post_type => artists,     posts_per_page => -1,);$artist_query ...
DISPLAY ARTISTSWITH LOCATIONS<?php //artists posts$artist_args=array(     post_type => artists,     posts_per_page => -1,)...
DISPLAY ARTISTSWITH LOCATIONS<?php //artists posts$artist_args=array(     post_type => artists,     posts_per_page => -1,)...
DISPLAYRELEASES<?php //releases posts$release_args=array(     post_type => releases,     posts_per_page => -1,);$release_q...
DISPLAYRELEASES<?php //releases posts$release_args=array(     post_type => releases,     posts_per_page => -1,);$release_q...
HOW DO WE SHOW ARTISTS WITH RELEASES?
<?php //artists posts          $artist_args=array(               post_type => artists,DISPLAY        posts_per_page => -1,...
while ($artist_query->have_posts()) : $artist_query->the_post(); ?>DISPLAY     <article>              <?php the_post_thumb...
while ($artist_query->have_posts()) : $artist_query->the_post(); ?>DISPLAY             <article>                      <?ph...
while ($artist_query->have_posts()) : $artist_query->the_post(); ?>DISPLAY             <article>                      <?ph...
while ($artist_query->have_posts()) : $artist_query->the_post(); ?>DISPLAY             <article>                      <?ph...
<?php //artists posts                   $artist_args=array(                         post_type => artists,START            ...
RESOURCESWordPress Codex & Forumshttp://codex.wordpress.org/http://wordpress.org/support/Register Custom Post Typeshttp://...
RESOURCESPluginsCPT-onomies: Using Custom Post Types as Taxonomieshttp://wordpress.org/extend/plugins/cpt-onomies/
DEMYSTIFYINGCUSTOM POSTTYPES &TAXONOMIESTracey Kemp@dogshindlegFull code samples and explanationsdogshindleg.com
Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012
Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012
Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012
Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney 2012
Upcoming SlideShare
Loading in...5
×

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

3,308

Published on

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

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,308
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
42
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

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

  1. 1. DEMYSTIFYINGCUSTOM POSTTYPES &TAXONOMIESTracey Kemp@dogshindleg
  2. 2. ➡ Registering Custom Post Types and Taxonomies➡ A plugin that can help➡ Displaying content from custom post types and taxonomies➡ Resources
  3. 3. FINAL RESULTA record label site withartists and their releases.
  4. 4. PLANNINGWhat will be a custom post type andwhat will be a taxonomy?Custom Post Type Taxonomy Custom Post Type Taxonomy TaxonomyArtist Location Release Artist YearThe Drones VIC Here Come The Lies The Drones 2002The Kill Devil Hills WA Wait Long By The River The Drones 2005Midget NSW / QLD Gala Mill The Drones 2006Front End Loader NSW Havilah The Drones 2008Dan Kelly VIC 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 Midget 1998 Understanding
  5. 5. REGISTERCUSTOMPOSTTYPES
  6. 6. REGISTER function register_custom_post_types() { // register post types hereCUSTOM }POST add_action(‘init’, ‘register_custom_post_types’);TYPES
  7. 7. function register_custom_post_types() { register_post_type( ‘artists’, array( ‘labels’ => array(REGISTER ‘name’ => _x(‘Artists’, ‘post type general name’), ‘singular_name’ => __( ‘Artist’, ‘taxonomy singular name’ ),ARTISTS ‘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’) ) ); } add_action(‘init’, ‘register_custom_post_types’);
  8. 8. function register_custom_post_types() { register_post_type( ‘artists’, array( ‘labels’ => array( ‘name’ => _x(‘Artists’, ‘post type general name’),REGISTER ‘singular_name’ => __( ‘Artist’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Artists’ ), ‘edit_item’ => __( ‘Edit Artist’ ),ARTISTS ‘add_new_item’ => __( ‘Add New Artist’ ), ‘new_item_name’ => __( ‘New Artist’ ), ), ‘public’ => true, ‘show_ui’ => true, ‘hierarchical’ => true, ‘supports’ => array (‘title’,‘editor’, ‘thumbnail’) ) );REGISTERRELEASES } add_action(‘init’, ‘register_custom_post_types’);
  9. 9. function register_custom_post_types() { register_post_type( ‘artists’, array( ‘labels’ => array( ‘name’ => _x(‘Artists’, ‘post type general name’),REGISTER ‘singular_name’ => __( ‘Artist’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Artists’ ), ‘edit_item’ => __( ‘Edit Artist’ ),ARTISTS ‘add_new_item’ => __( ‘Add New Artist’ ), ‘new_item_name’ => __( ‘New Artist’ ), ), ‘public’ => true, ‘show_ui’ => true, ‘hierarchical’ => true, ‘supports’ => array (‘title’,‘editor’, ‘thumbnail’) ) ); register_post_type( ‘releases’, array( ‘labels’ => array( ‘name’ => _x(‘Releases’, ‘post type general name’),REGISTER ‘singular_name’ => __( ‘Release’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Releases’ ),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’) ) ); } add_action(‘init’, ‘register_custom_post_types’);
  10. 10. function register_custom_post_types() { register_post_type( ‘artists’, array( ‘labels’ => array( ‘name’ => _x(‘Artists’, ‘post type general name’),REGISTER ‘singular_name’ => __( ‘Artist’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Artists’ ), ‘edit_item’ => __( ‘Edit Artist’ ),ARTISTS ‘add_new_item’ => __( ‘Add New Artist’ ), ‘new_item_name’ => __( ‘New Artist’ ), ), ‘public’ => true, ‘show_ui’ => true, ‘hierarchical’ => true, ‘supports’ => array (‘title’,‘editor’, ‘thumbnail’) ) ); register_post_type( ‘releases’, array( ‘labels’ => array( ‘name’ => _x(‘Releases’, ‘post type general name’),REGISTER ‘singular_name’ => __( ‘Release’, ‘taxonomy singular name’ ), ‘all_items’ => __( ‘All Releases’ ),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’) ) ); } add_action(‘init’, ‘register_custom_post_types’);
  11. 11. REGISTERTAXONOMIES
  12. 12. REGISTER function register_taxonomies() {TAXO // register post types hereNOMIES } add_action(‘init’, ‘register_taxonomies’, 0);
  13. 13. function register_taxonomies() { $labels = array( ‘name’ => _x( ‘Location’, ‘taxonomy general name’ ), ‘singular_name’ => _x( ‘Location’, ‘taxonomy singular name’ ),REGISTER ‘all_items’ => __( ‘All Locations’ ), ‘edit_item’ => __( ‘Edit Location’ ),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);
  14. 14. 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’ ),REGISTER ‘add_new_item’ => __( ‘Add New Location’ ), ‘menu_name’ => __( ‘Locations’ ), );LOCATION register_taxonomy( ‘locations’, array(‘artists’), // post-type(s) array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) );REGISTERRELEASEYEAR } add_action(‘init’, ‘register_taxonomies’, 0);
  15. 15. 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’ ),REGISTER ‘add_new_item’ => __( ‘Add New Location’ ), ‘menu_name’ => __( ‘Locations’ ), );LOCATION register_taxonomy( ‘locations’, array(‘artists’), // post-type(s) array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) ); $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 ); register_taxonomy( ‘release-year’,RELEASE array(‘releases’), // post-type(s) array(YEAR ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) ); } add_action(‘init’, ‘register_taxonomies’, 0);
  16. 16. 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’ ),REGISTER ‘add_new_item’ => __( ‘Add New Location’ ), ‘menu_name’ => __( ‘Locations’ ), );LOCATION register_taxonomy( ‘locations’, array(‘artists’), // post-type(s) array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) ); $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 ); register_taxonomy( ‘release-year’,RELEASE array(‘releases’), // post-type(s) array(YEAR ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ) ); } add_action(‘init’, ‘register_taxonomies’, 0);
  17. 17. USING ACUSTOM POSTTYPE AS ATAXONOMY
  18. 18. DISPLAYARTISTS
  19. 19. DISPLAYARTISTS<?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(); ?>
  20. 20. DISPLAY ARTISTSWITH 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 echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?> </article><?php endwhile; } wp_reset_query(); ?>
  21. 21. DISPLAY ARTISTSWITH LOCATIONS<?php //artists posts$artist_args=array( post_type => artists, posts_per_page => -1,); <?php echo get_the_term_list( $id, $taxonomy, $before, $sep, $after ) ?>$artist_query = null;$artist_query = new WP_Query($artist_args); <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?>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 endwhile; } wp_reset_query(); ?>
  22. 22. DISPLAYRELEASES<?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(); ?>
  23. 23. DISPLAYRELEASES<?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(); ?>
  24. 24. HOW DO WE SHOW ARTISTS WITH RELEASES?
  25. 25. <?php //artists posts $artist_args=array( post_type => artists,DISPLAY posts_per_page => -1,ARTISTS ); $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(); ?>
  26. 26. while ($artist_query->have_posts()) : $artist_query->the_post(); ?>DISPLAY <article> <?php the_post_thumbnail(); ?>ARTISTS <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,DISPLAY ); $release_query = null;RELEASES $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>
  27. 27. while ($artist_query->have_posts()) : $artist_query->the_post(); ?>DISPLAY <article> <?php the_post_thumbnail(); ?>ARTISTS <h2><?php the_title(); ?></h2> <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?>GET ARTIST TITLE <?php //releases posts $release_args=array( post_type => releases, posts_per_page => -1, );DISPLAY $release_query = null; $release_query = new WP_Query($release_args);RELEASES 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>
  28. 28. while ($artist_query->have_posts()) : $artist_query->the_post(); ?>DISPLAY <article> <?php the_post_thumbnail(); ?>ARTISTS <h2><?php the_title(); ?></h2> <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?>GET ARTIST TITLE <?php $tax_artist = get_the_title(); ?> <?php //releases posts $release_args=array( post_type => releases, posts_per_page => -1, );DISPLAY $release_query = null; $release_query = new WP_Query($release_args);RELEASES 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>
  29. 29. while ($artist_query->have_posts()) : $artist_query->the_post(); ?>DISPLAY <article> <?php the_post_thumbnail(); ?>ARTISTS <h2><?php the_title(); ?></h2> <?php echo get_the_term_list( $post->ID, ‘locations’, ‘From: ’, ‘ / ‘, ‘’ ); ?> <?php the_excerpt(); ?>GET ARTIST TITLE <?php $tax_artist = get_the_title(); ?> <?php //releases posts $release_args=array( post_type => releases,$taxonomy => $term artists => $tax_artist, posts_per_page => -1, ); $release_query = null; $release_query = new WP_Query($release_args); if( $release_query->have_posts() ) {DISPLAY while ($release_query->have_posts()) : $release_query->the_post(); ?>RELEASES <article> <?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php echo get_the_term_list( $post->ID, ‘release-year’, ‘(’, ‘‘, ‘)’ ); ? </article>
  30. 30. <?php //artists posts $artist_args=array( post_type => artists,START ); posts_per_page => -1,ARTISTS $artist_query = null; $artist_query = new WP_Query($artist_args);QUERY 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(); ?>GET ARTIST TITLE <?php $tax_artist = get_the_title(); ?> <?php //releases posts $release_args=array(START RELEASES QUERY post_type => releases,$taxonomy => $term artists => $tax_artist, 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>CLOSE RELEASES <?php endwhile; } wp_reset_query(); ?>CLOSE </article>ARTISTS <?php endwhile; } wp_reset_query(); ?>
  31. 31. RESOURCESWordPress Codex & Forumshttp://codex.wordpress.org/http://wordpress.org/support/Register Custom Post Typeshttp://codex.wordpress.org/Function_Reference/register_post_typeRegister Taxonomieshttp://codex.wordpress.org/Function_Reference/register_taxonomyQuery Postshttp://codex.wordpress.org/Function_Reference/query_postsThe Loophttp://codex.wordpress.org/The_Loop
  32. 32. RESOURCESPluginsCPT-onomies: Using Custom Post Types as Taxonomieshttp://wordpress.org/extend/plugins/cpt-onomies/
  33. 33. DEMYSTIFYINGCUSTOM POSTTYPES &TAXONOMIESTracey Kemp@dogshindlegFull code samples and explanationsdogshindleg.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×