Creatively creating custom post types! word sesh2

634 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
634
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Creatively creating custom post types! word sesh2

  1. 1. Creatively Creating Custom Post Types Nikhil Vimal #WordSesh2
  2. 2. Hi, I’m Nikhil (NikV) • I develop with WordPress • I can be found on Twitter @TechVoltz • Core contributor for WordPress 3.7
  3. 3. Custom Post Types? They Rock (Seriously)
  4. 4. But what are Custom Post Types? An example of a post type is posts and pages
  5. 5. Portfolio Post Type could be paintings
  6. 6. Online Store Post Type could be Products
  7. 7. Your only limitation is Your imagination
  8. 8. “WordPress can hold and display many different types of content.” -WordPress Codex
  9. 9. Custom Post types should be added with… A plugin of course
  10. 10. 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 */
  11. 11. 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 ); }
  12. 12. Now lets add to our Custom Post Type
  13. 13. Custom meta data With meta boxes
  14. 14. 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');
  15. 15. '<input type="text" name=“cpt_meta" value="' . $cpt_meta . '" class="widefat" />';
  16. 16. You can also use… User roles and Capabilities with Custom Post Types
  17. 17. 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');
  18. 18. Capabilities with Custom Post Types
  19. 19. Adding Capabilities to roles
  20. 20. Templates For Custom Post Types
  21. 21. Styling your CPT Page With single-$posttype.php
  22. 22. Having an Archive Page for your CPT With archive-$posttype.php
  23. 23. Taxonomies More Organization
  24. 24. “Basically, a taxonomy is a way to group things together” -WordPress Codex
  25. 25. Taxonomies Categories and Tags
  26. 26. Portfolio Taxonomy is oil painting
  27. 27. add_action('init','wordsesh_tracks‘);
  28. 28. function wordsesh_tracks(){ $tracks_args = array( 'hierarchical' => true, 'query_var' => 'tracks', 'show_tagcloud' => true,
  29. 29. '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', ), );
  30. 30. register_taxonomy('tracks', array('wordsesh'), $tracks_args); The array(‘wordsesh’) is our custom post type
  31. 31. Resources • http://justintadlock.com/archives/2010/04/29/custom-post-types-inwordpress • http://codex.wordpress.org/Post_Types • http://codex.wordpress.org/Taxonomies • http://wp.smashingmagazine.com/2012/11/08/complete-guidecustom-post-types/
  32. 32. Thank You! Nikhil Vimal (NikV) @TechVoltz

×