creatively creating custom post types!

36
Creatively Creating Custom Post Types Nikhil Vimal #WordUpMPLS

Upload: techvoltz

Post on 12-Jul-2015

98 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Creatively creating custom post types!

Creatively Creating Custom Post TypesNikhil Vimal

#WordUpMPLS

Page 2: Creatively creating custom post types!

Hi, I’m Nikhil

• I develop with WordPress• I can be found on Twitter @TechVoltz• This my first talk ever

Page 3: Creatively creating custom post types!

Custom Post Types?They Rock (Seriously)

Page 4: Creatively creating custom post types!

But what are they?

• Instead of using default posts and categories in WordPress, use Custom Post Types

•You can add a lot of cool things that are not in a Default WordPress install

•Added in WordPress 3.0

Page 5: Creatively creating custom post types!

Why Do You need them?Organization

Page 6: Creatively creating custom post types!

PortfolioPost Type could be paintings

Page 7: Creatively creating custom post types!

Online StorePost Type could be Products

Page 8: Creatively creating custom post types!

Your only limitation isYour imagination

Page 9: Creatively creating custom post types!

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

-WordPress Codex

Page 10: Creatively creating custom post types!

But how do we add Custom Post Types?With a plugin of course

Page 11: Creatively creating custom post types!

Create a file called myposttype.php

<?php/*** Plugin Name: Your Custom Post Type* 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 12: Creatively creating custom post types!

add_action('init', 'wordup_sessions');

function wordup_sessions() {$wordup_args = array(

'public' => true,'has_archive' => true,'query_var' => 'wordup','menu_position' => 5,'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),

'labels' => array('name' => 'WordUp Sessions','singular' => 'WordUp 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 WordUp','not_found' => 'No sessions found','not_found_in_trash' => 'No Sessions found in the Trash',

),);

register_post_type('WordUp', $wordup_args );

}

Page 13: Creatively creating custom post types!

Lets examine all the code!!

Page 14: Creatively creating custom post types!

add_action('init', 'wordup_sessions');

Page 15: Creatively creating custom post types!

function wordup_sessions() {$wordup_args = array(

'public' => true,'has_archive' => true,'query_var' => 'wordup', 'menu_position' => 5,'supports' => array( 'title', 'editor', 'thumbnail',

'excerpt', 'comments' ),

Page 16: Creatively creating custom post types!

'labels' => array(

'name' => 'WordUp Sessions',

'singular' => 'WordUp 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',

),

);

Page 17: Creatively creating custom post types!

register_post_type('WordUp', $wordup_args );

}

Page 18: Creatively creating custom post types!

Styling your CPT PageWith single-$posttype.php

Page 19: Creatively creating custom post types!

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

Page 20: Creatively creating custom post types!
Page 21: Creatively creating custom post types!
Page 22: Creatively creating custom post types!
Page 23: Creatively creating custom post types!

TaxonomiesMore Organization

Page 24: Creatively creating custom post types!

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

-WordPress Codex

Page 25: Creatively creating custom post types!

TaxonomiesCategories and Tags

Page 26: Creatively creating custom post types!

PortfolioTaxonomy is oil painting

Page 27: Creatively creating custom post types!

After our Custom Post type function….

Page 28: Creatively creating custom post types!

add_action('init', 'wordup_tracks');

Page 29: Creatively creating custom post types!

function wordup_tracks(){

$track_args = array(

'hierarchical' => true,

'query_var' => ‘wordup_tracks',

'show_tagcloud' => true,

Page 30: Creatively creating custom post types!

'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 31: Creatively creating custom post types!

register_taxonomy(‘wordup_tracks', array('wordup'), $dev_args);

}

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

Page 32: Creatively creating custom post types!
Page 33: Creatively creating custom post types!

You can add moreLike Custom Fields

Page 34: Creatively creating custom post types!

Questions?

Page 35: Creatively creating custom post types!

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 36: Creatively creating custom post types!

Thank You!Nikhil Vimal@TechVoltz