Slide 1

Slide 1 text

Creatively Creating Custom Post Types Nikhil Vimal #WordSesh2

Slide 2

Slide 2 text

Hi, I’m Nikhil (NikV) • I develop with WordPress • I can be found on Twitter @TechVoltz • Core contributor for WordPress 3.7

Slide 3

Slide 3 text

Custom Post Types? They Rock (Seriously)

Slide 4

Slide 4 text

But what are Custom Post Types? An example of a post type is posts and pages

Slide 5

Slide 5 text

Portfolio Post Type could be paintings

Slide 6

Slide 6 text

Online Store Post Type could be Products

Slide 7

Slide 7 text

Your only limitation is Your imagination

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Create a file called myposttype.php

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Now lets add to our Custom Post Type

Slide 13

Slide 13 text

Custom meta data With meta boxes

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

'';

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Capabilities with Custom Post Types

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Adding Capabilities to roles

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Templates For Custom Post Types

Slide 24

Slide 24 text

Styling your CPT Page With single-$posttype.php

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Taxonomies More Organization

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Taxonomies Categories and Tags

Slide 31

Slide 31 text

Portfolio Taxonomy is oil painting

Slide 32

Slide 32 text

add_action('init','wordsesh_tracks‘);

Slide 33

Slide 33 text

function wordsesh_tracks(){ $tracks_args = array( 'hierarchical' => true, 'query_var' => 'tracks', 'show_tagcloud' => true,

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

register_taxonomy('tracks', array('wordsesh'), $tracks_args); The array(‘wordsesh’) is our custom post type

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

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/

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Thank You! Nikhil Vimal (NikV) @TechVoltz