Creatively creating custom post types! word sesh2
-
Upload
techvoltz -
Category
Technology
-
view
358 -
download
0
Transcript of Creatively creating custom post types! word sesh2
![Page 1: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/1.jpg)
Creatively Creating Custom Post TypesNikhil Vimal
#WordSesh2
![Page 2: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/3.jpg)
Custom Post Types?They Rock (Seriously)
![Page 4: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/4.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/5.jpg)
PortfolioPost Type could be paintings
![Page 6: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/6.jpg)
Online StorePost Type could be Products
![Page 7: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/7.jpg)
Your only limitation isYour imagination
![Page 8: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/8.jpg)
“WordPress can hold and display many different types of content.”
-WordPress Codex
![Page 9: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/9.jpg)
Custom Post types should be added with…A plugin of course
![Page 10: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/12.jpg)
Now lets add to our Custom Post Type
![Page 13: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/13.jpg)
Custom meta dataWith meta boxes
![Page 14: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/15.jpg)
'<input type="text" name=“cpt_meta" value="' . $cpt_meta . '" class="widefat" />';
![Page 16: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/16.jpg)
You can also use…User roles and Capabilities with Custom Post Types
![Page 17: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/18.jpg)
![Page 19: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/19.jpg)
Capabilities with Custom Post Types
![Page 20: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/20.jpg)
![Page 21: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/21.jpg)
Adding Capabilities to roles
![Page 22: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/22.jpg)
![Page 23: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/23.jpg)
TemplatesFor Custom Post Types
![Page 24: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/24.jpg)
Styling your CPT PageWith single-$posttype.php
![Page 25: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/25.jpg)
Having an Archive Page for your CPTWith archive-$posttype.php
![Page 26: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/26.jpg)
![Page 27: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/27.jpg)
![Page 28: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/28.jpg)
TaxonomiesMore Organization
![Page 29: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/29.jpg)
“Basically, a taxonomy is a way to group things together”
-WordPress Codex
![Page 30: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/30.jpg)
TaxonomiesCategories and Tags
![Page 31: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/31.jpg)
PortfolioTaxonomy is oil painting
![Page 32: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/32.jpg)
add_action('init','wordsesh_tracks‘);
![Page 33: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/33.jpg)
function wordsesh_tracks(){
$tracks_args = array(
'hierarchical' => true,
'query_var' => 'tracks',
'show_tagcloud' => true,
![Page 34: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/34.jpg)
'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](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/35.jpg)
register_taxonomy('tracks', array('wordsesh'), $tracks_args);
The array(‘wordsesh’) is our custom post type
![Page 36: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/36.jpg)
![Page 37: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/37.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/38.jpg)
![Page 39: Creatively creating custom post types! word sesh2](https://reader034.fdocuments.in/reader034/viewer/2022052315/554bdc4db4c9058f6c8b569e/html5/thumbnails/39.jpg)
Thank You!Nikhil Vimal (NikV)@TechVoltz