Wp meetup custom post types

Post on 07-Nov-2014

3801 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

Transcript

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

!WordPress Developers Unite!

Not your regular WordCamp

Speakers announced today

Tickets: $85www.wordcampdevelopers.com

?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

#CREATING A NEW CUSTOM POST TYPE

#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);}

#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' => '' );

#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' );

#CREATING A NEW CUSTOM POST TYPE

#CREATING CUSTOM TAXONOMIES

#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 ) );}

#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' ),);

#CREATING CUSTOM TAXONOMIES

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

?CUSTOM POST TYPE ICONS

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

#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 }

#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 }

#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 }

?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

?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' );

?CUSTOM POST TYPE PAGES

?CUSTOM POST TYPE PAGES

?CUSTOM POST TYPE PAGES

?CUSTOM POST TYPE PAGES

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

#“FUN” WITH QUERIES

#“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

#“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>

?EXPAND YOUR HORIZONS

Make Web Not WarSaturday May 7th

www.webnotwar.ca

!WordPress Developers Unite!

Not your regular WordCamp

Speakers announced today

Tickets: $85www.wordcampdevelopers.com

GETINTOUCH

Morten Rand-HendriksenCreative Director, Pink & Yellow Media

www.designisphilosophy.com

@mor10

www.pinkandyellow.com

designisphilosophy.com/facebook

top related