Wp meetup custom post types

44
HELLO, MY NAME IS MOR10 people tend to spell it with an ‘o’, but that’s just plain wrong Custom Post Types in WordPress 3.x Setup, Customization and Useage Scenarios Morten Rand-Hendriksen Creative Director, Pink & Yellow Media www.designisphilosophy.com Twitter: @mor10

description

Slides from my Vancouver WordPress Meetup presentation, March 31st, 2011

Transcript of Wp meetup custom post types

Page 1: Wp meetup custom post types

HELLO,MY NAME IS MOR10

people tend to spell it with an ‘o’, but that’s just plain wrong

Custom Post Types in WordPress 3.xSetup, Customization and Useage Scenarios

Morten Rand-HendriksenCreative Director, Pink & Yellow Media

www.designisphilosophy.comTwitter: @mor10

Page 2: Wp meetup custom post types

!WordPress Developers Unite!

Not your regular WordCamp

Speakers announced today

Tickets: $85www.wordcampdevelopers.com

Page 3: Wp meetup custom post types

?WHAT ARE CUSTOM POST TYPES

• Like “regular” WordPress posts, but handled separately.

• Custom single template• Custom index page• Custom taxonomies (categories

and tags)• Custom taxonomy indexes• Advanced custom queries

Page 4: Wp meetup custom post types
Page 5: Wp meetup custom post types

#CREATING A NEW CUSTOM POST TYPE

Page 6: Wp meetup custom post types

#CREATING A NEW CUSTOM POST TYPE

// Add new post type for Shootsadd_action('init', 'adley_shoots_init');function adley_shoots_init() { $labels = array( … stuff … ); $args = array( … stuff … ); register_post_type('shoot',$args);}

Page 7: Wp meetup custom post types

#CREATING A NEW CUSTOM POST TYPE

$labels = array( 'name' => _x('Shoots', 'general name'), 'singular_name' => _x('Shoot', 'singular name'), 'add_new' => _x('Add new shoot', 'shoot'), 'add_new_item' => __('Add new shoot'), 'edit_item' => __('Edit shoot'), 'new_item' => __('New shoot'), 'view_item' => __('View shoot'), 'search_items' => __('Search shoots'), 'not_found' => __('No shoots found'), 'not_found_in_trash' => __('No shoots found in trash'), 'parent_item_colon' => '' );

Page 8: Wp meetup custom post types

#CREATING A NEW CUSTOM POST TYPE

$args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title','editor','author','thumbnail','excerpt','comments'), 'has_archive' => 'shoots' );

Page 9: Wp meetup custom post types

#CREATING A NEW CUSTOM POST TYPE

Page 10: Wp meetup custom post types
Page 11: Wp meetup custom post types
Page 12: Wp meetup custom post types
Page 13: Wp meetup custom post types

#CREATING CUSTOM TAXONOMIES

Page 14: Wp meetup custom post types

#CREATING CUSTOM TAXONOMIES

// Add taxonomies for Shootsadd_action( 'init', 'adley_create_taxonomies', 0 );

function adley_create_taxonomies() {

$labels = array( … stuff …);

register_taxonomy('topics','shoot',array( … stuff ) );}

Page 15: Wp meetup custom post types

#CREATING CUSTOM TAXONOMIES

$labels = array('name' => _x( 'Topics', 'taxonomy general name' ),'singular_name' => _x( 'Topic', 'taxonomy singular name' ),'search_items' => __( 'Search by topic' ),'all_items' => __( 'All topics' ),'most_used_items' => null,'parent_item' => null,'parent_item_colon' => null,'edit_item' => __( 'Change topic' ), 'update_item' => __( 'Update topic' ),'add_new_item' => __( 'Add new topic' ),'new_item_name' => __( 'New topic' ),'menu_name' => __( 'Topics' ),);

Page 16: Wp meetup custom post types

#CREATING CUSTOM TAXONOMIES

register_taxonomy('topics','shoot',array('hierarchical' => true,'labels' => $labels,'show_ui' => true,'query_var' => true,'rewrite' => array('slug' => 'topics' )));

Page 17: Wp meetup custom post types
Page 18: Wp meetup custom post types
Page 19: Wp meetup custom post types

?CUSTOM POST TYPE ICONS

Page 20: Wp meetup custom post types

http://randyjensenonline.com/thoughts/wordpress-custom-post-type-fugue-icons/

Page 21: Wp meetup custom post types
Page 22: Wp meetup custom post types

#CUSTOM POST TYPE ICONS

// Adds custom icon to the Shoots tabadd_action( 'admin_head', 'cpt_icons' );function cpt_icons() { ?> <style type="text/css" media="screen"> #menu-posts-shoot .wp-menu-image { background: url(<?php bloginfo('template_url') ?>/images/photo-album.png) no-repeat 6px – 17px !important; } #menu-posts-shoot:hover .wp-menu-image, #menu-posts-shoot.wp-has-current-submenu .wp-menu-image { background-position:6px 7px!important; } </style><?php }

Page 23: Wp meetup custom post types

#CUSTOM POST TYPE ICONS

// Adds custom icon to the Shoots tabadd_action( 'admin_head', 'cpt_icons' );function cpt_icons() { ?> <style type="text/css" media="screen"> #menu-posts-shoot .wp-menu-image { background: url(<?php bloginfo('template_url') ?>/images/photo-album.png) no-repeat 6px – 17px !important; } #menu-posts-shoot:hover .wp-menu-image, #menu-posts-shoot.wp-has-current-submenu .wp-menu-image { background-position:6px 7px!important; } </style><?php }

Page 24: Wp meetup custom post types

#CUSTOM POST TYPE ICONS

// Adds custom icon to the Shoots tabadd_action( 'admin_head', 'cpt_icons' );function cpt_icons() { ?> <style type="text/css" media="screen"> #menu-posts-shoot .wp-menu-image { background: url(<?php bloginfo('template_url') ?>/images/photo-album.png) no-repeat 6px – 17px !important; } #menu-posts-shoot:hover .wp-menu-image, #menu-posts-shoot.wp-has-current-submenu .wp-menu-image { background-position:6px 7px!important; } </style><?php }

Page 25: Wp meetup custom post types

?CUSTOM POST TYPE PAGES

For single pages:single-postType.php

For taxonomy pages:taxonomy.php

For index page(s) (new in 3.1):archive-postType.php

Page 26: Wp meetup custom post types

?CUSTOM POST TYPE PAGES

$args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title','editor','author','thumbnail','excerpt','comments'), 'has_archive' => 'shoots' );

Page 27: Wp meetup custom post types

?CUSTOM POST TYPE PAGES

Page 28: Wp meetup custom post types

?CUSTOM POST TYPE PAGES

Page 29: Wp meetup custom post types

?CUSTOM POST TYPE PAGES

Page 30: Wp meetup custom post types

?CUSTOM POST TYPE PAGES

Page 31: Wp meetup custom post types
Page 32: Wp meetup custom post types
Page 33: Wp meetup custom post types
Page 34: Wp meetup custom post types

single-soknad.php archive-soknad.php taxonomy.php

Page 35: Wp meetup custom post types

#“FUN” WITH QUERIES

Page 36: Wp meetup custom post types
Page 37: Wp meetup custom post types
Page 38: Wp meetup custom post types
Page 39: Wp meetup custom post types

#“FUN” WITH QUERIES

Get the object term (taxonomy value)

<?php

$terms = wp_get_object_terms(

$post->ID,

'soknadsstatus',

'fields=names‘

);

$status = implode(', ', $terms);

?>

Taxonomy slug

all, ids, names or all_with_object_id

Page 40: Wp meetup custom post types
Page 41: Wp meetup custom post types

#“FUN” WITH QUERIES

Create list if term items (categories)

<?php

$kommuneterm = get_terms('kommune', 'orderby=name‘); ?>

<ul><?php foreach ($kommuneterm as $sted) { ?>

<li><a href=“<?php echo get_term_link( $sted->slug, 'kommune‘ );?>"><?php echo $sted->name; ?> (<?php echo $sted->count; ?>)</a></li><?php } ?></ul>

Page 42: Wp meetup custom post types

?EXPAND YOUR HORIZONS

Make Web Not WarSaturday May 7th

www.webnotwar.ca

Page 43: Wp meetup custom post types

!WordPress Developers Unite!

Not your regular WordCamp

Speakers announced today

Tickets: $85www.wordcampdevelopers.com

Page 44: Wp meetup custom post types

GETINTOUCH

Morten Rand-HendriksenCreative Director, Pink & Yellow Media

www.designisphilosophy.com

@mor10

www.pinkandyellow.com

designisphilosophy.com/facebook