creatively creating custom post types! word sesh2
TRANSCRIPT
Creatively Creating Custom Post TypesNikhil Vimal
#WordSesh2
Hi, I’m Nikhil (NikV)
• I develop with WordPress• I can be found on Twitter @TechVoltz• Core contributor for WordPress 3.7
Custom Post Types?They Rock (Seriously)
But what are Custom Post Types?
An example of a post type is posts and pages
PortfolioPost Type could be paintings
Online StorePost Type could be Products
Your only limitation isYour imagination
“WordPress can hold and display many different types of content.”
-WordPress Codex
Custom Post types should be added with…A plugin of course
Create a file called myposttype.php
<?php/*** Plugin Name: Your Custom Post Type plugin* Plugin URI: http://yourpluginswebsite.com* Description: A brief description of your Plugin.* Version: The Plugin's Version Number, e.g.: 1.0* Author: Your Name* Author URI: http://yourwebsite.com* License: A "Slug" license name e.g. GPL2*/
add_action('init', 'wordsesh_sessions');
function wordsesh_sessions() {$wordsesh_args = array(
'public' => true,'query_var' => 'wordsesh',‘can_export’ => true,'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'page-attributes' ),
'labels' => array('name' => 'WordSesh 2 Sessions','singular' => 'WordSesh 2 Session','add_new' => 'Add Session','add_new_item' => 'Add Session','edit_item' => 'Edit Session','new_item' => 'New Session','view_item' => 'View Session','search_items' => 'Search Sessions','not_found' => 'No sessions found','not_found_in_trash' => 'No Sessions found in the Trash',
),);
register_post_type('WordSesh', $wordsesh_args );
}
Now lets add to our Custom Post Type
Custom meta dataWith meta boxes
Let’s add a meta box
function add_cpt_metabox(){
add_meta_box('cpt_meta', 'Speaker Meta Box', 'cpt_meta', 'wordsesh','side', 'default');
}
add_action('add_meta_boxes', 'add_cpt_metabox');
'<input type="text" name=“cpt_meta" value="' . $cpt_meta . '" class="widefat" />';
You can also use…User roles and Capabilities with Custom Post Types
function delete_wordseshcpt_menu() {
if( !current_user_can( 'administrator' ) ):
remove_menu_page('edit.php?post_type=wordsesh');
endif;
}
add_action('admin_menu', 'delete_wordseshcpt_menu');
Capabilities with Custom Post Types
Adding Capabilities to roles
TemplatesFor Custom Post Types
Styling your CPT PageWith single-$posttype.php
Having an Archive Page for your CPTWith archive-$posttype.php
TaxonomiesMore Organization
“Basically, a taxonomy is a way to group things together”
-WordPress Codex
TaxonomiesCategories and Tags
PortfolioTaxonomy is oil painting
add_action('init','wordsesh_tracks‘);
function wordsesh_tracks(){
$tracks_args = array(
'hierarchical' => true,
'query_var' => 'tracks',
'show_tagcloud' => true,
'labels' => array('name' => 'Tracks','edit_item' => 'Edit Track','update_item' => 'Update Track','add_new_item' => 'Add New Track','new_item_name' => 'New Track','all_items' => 'All Tracks','search_items' => 'Search Tracks','popular_items' => 'Popular Tracks','add_or_remove_items' => 'Add or remove Tracks','choose_from_most_used' => 'Choose from most used Tracks',
),
);
register_taxonomy('tracks', array('wordsesh'), $tracks_args);
The array(‘wordsesh’) is our custom post type
Resources• http://justintadlock.com/archives/2010/04/29/custom-post-types-in-
wordpress
• http://codex.wordpress.org/Post_Types
• http://codex.wordpress.org/Taxonomies
• http://wp.smashingmagazine.com/2012/11/08/complete-guide-custom-post-types/
Thank You!Nikhil Vimal (NikV)@TechVoltz