+ All Categories
Home > Technology > Creatively creating custom post types! word sesh2

Creatively creating custom post types! word sesh2

Date post: 08-May-2015
Category:
Upload: techvoltz
View: 355 times
Download: 0 times
Share this document with a friend
39
Creatively Creating Custom Post Types Nikhil Vimal #WordSesh2
Transcript
Page 1: Creatively creating custom post types!  word sesh2

Creatively Creating Custom Post TypesNikhil Vimal

#WordSesh2

Page 2: Creatively creating custom post types!  word sesh2

Hi, I’m Nikhil (NikV)

• I develop with WordPress• I can be found on Twitter @TechVoltz• Core contributor for WordPress 3.7

Page 3: Creatively creating custom post types!  word sesh2

Custom Post Types?They Rock (Seriously)

Page 4: Creatively creating custom post types!  word sesh2

But what are Custom Post Types?

An example of a post type is posts and pages

Page 5: Creatively creating custom post types!  word sesh2

PortfolioPost Type could be paintings

Page 6: Creatively creating custom post types!  word sesh2

Online StorePost Type could be Products

Page 7: Creatively creating custom post types!  word sesh2

Your only limitation isYour imagination

Page 8: Creatively creating custom post types!  word sesh2

“WordPress can hold and display many different types of content.”

-WordPress Codex

Page 9: Creatively creating custom post types!  word sesh2

Custom Post types should be added with…A plugin of course

Page 10: Creatively creating custom post types!  word sesh2

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*/

Page 11: Creatively creating custom post types!  word sesh2

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

}

Page 12: Creatively creating custom post types!  word sesh2

Now lets add to our Custom Post Type

Page 13: Creatively creating custom post types!  word sesh2

Custom meta dataWith meta boxes

Page 14: Creatively creating custom post types!  word sesh2

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

Page 15: Creatively creating custom post types!  word sesh2

'<input type="text" name=“cpt_meta" value="' . $cpt_meta . '" class="widefat" />';

Page 16: Creatively creating custom post types!  word sesh2

You can also use…User roles and Capabilities with Custom Post Types

Page 17: Creatively creating custom post types!  word sesh2

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

Page 18: Creatively creating custom post types!  word sesh2
Page 19: Creatively creating custom post types!  word sesh2

Capabilities with Custom Post Types

Page 20: Creatively creating custom post types!  word sesh2
Page 21: Creatively creating custom post types!  word sesh2

Adding Capabilities to roles

Page 22: Creatively creating custom post types!  word sesh2
Page 23: Creatively creating custom post types!  word sesh2

TemplatesFor Custom Post Types

Page 24: Creatively creating custom post types!  word sesh2

Styling your CPT PageWith single-$posttype.php

Page 25: Creatively creating custom post types!  word sesh2

Having an Archive Page for your CPTWith archive-$posttype.php

Page 26: Creatively creating custom post types!  word sesh2
Page 27: Creatively creating custom post types!  word sesh2
Page 28: Creatively creating custom post types!  word sesh2

TaxonomiesMore Organization

Page 29: Creatively creating custom post types!  word sesh2

“Basically, a taxonomy is a way to group things together”

-WordPress Codex

Page 30: Creatively creating custom post types!  word sesh2

TaxonomiesCategories and Tags

Page 31: Creatively creating custom post types!  word sesh2

PortfolioTaxonomy is oil painting

Page 32: Creatively creating custom post types!  word sesh2

add_action('init','wordsesh_tracks‘);

Page 33: Creatively creating custom post types!  word sesh2

function wordsesh_tracks(){

$tracks_args = array(

'hierarchical' => true,

'query_var' => 'tracks',

'show_tagcloud' => true,

Page 34: Creatively creating custom post types!  word sesh2

'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',

),

);

Page 35: Creatively creating custom post types!  word sesh2

register_taxonomy('tracks', array('wordsesh'), $tracks_args);

The array(‘wordsesh’) is our custom post type

Page 36: Creatively creating custom post types!  word sesh2
Page 37: Creatively creating custom post types!  word sesh2

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/

Page 38: Creatively creating custom post types!  word sesh2
Page 39: Creatively creating custom post types!  word sesh2

Thank You!Nikhil Vimal (NikV)@TechVoltz


Recommended