• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Custom Post Types in WP3
 

Custom Post Types in WP3

on

  • 2,708 views

I gave a brief overview of the new Custom Post Types in WordPress 3. I talked about how we can use them and did a demo of creating our 1st Custom Pots Type plugin. The plugin can be downloaded from ...

I gave a brief overview of the new Custom Post Types in WordPress 3. I talked about how we can use them and did a demo of creating our 1st Custom Pots Type plugin. The plugin can be downloaded from http://www.tripleginteractive.com/blog/wordpress/wordcamp-columbus-custom-post-types/

Statistics

Views

Total Views
2,708
Views on SlideShare
2,708
Embed Views
0

Actions

Likes
0
Downloads
13
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Custom Post Types in WP3 Custom Post Types in WP3 Presentation Transcript

    • Custom Post Types & TaxonomiesGregg Henrytriple G interactive @GreggHenry
    • Hello. · BS in Computer Science from Bowling Green State University. · 10 Years of Web Development Experience. · 5 Years of Coding WordPress Themes and Plugins. · 3 Years of running triple G interactive.
    • So...What is a Custom Post Type? · The same way you can create regular posts, you can create custom ones with more creative control. · Not just a custom post, but custom content. · Been around since WP2.9.Examples · Portfolio, Video Library, Testimonials, FAQs, Recipes, etc.Advantanges · Easier on developers; no more relying on custom fields. · Easier for your clients to use; have their own navigation link.Take Note · Don’t get carried away with custom post types!
    • Wait...What is a Taxonomy? · Put in very easy to understand words - its just a way to classify and group things. · Built in taxonomies are Categories and Tags · Been around since WP 2.3, but now extremely handy with Custom Post TypesCustom Taxonomies Example · genres for books
    • Our 1st Plugin with Custom Post Types· Create a Recipe Class to house our plugin· Register the post type ‘recipe’· Add Custom Meta Box (no more ugly custom fields)· Create custom categories using taxonomies· Tips on how to display the new content we have· Profit?
    • Step 1: Let’s Get Started· Create your folder recipress in wp-content/plugins/· Create recipress.php in wp-content/plugins/recipress/<?php/*Plugin Name: ReciPressPlugin URI: http://www.tripleginteractive.com/Description: Add a basic Recipe Plugin for your website using custom post types.Author: Gregg HenryVersion: 1.0Author URI: http://www.tripleginteractive.com/*/
    • Step 2: Create The Class to house our Plugin· define any constant variables· give the class a unique namedefine(RECIPRESS_POST_TYPE, ‘recipe’);class Recipress{}
    • Step 3: Tap into Plugin Activation Hook· register_activation_hook function lets us run specific code when the plugin is activat-ed.· We need to flush the rewrite rulesdefine(RECIPRESS_POST_TYPE, ‘recipe’);class Recipress{ function Install() { flush_rewrite_rules(); }} //end Recipress Classregister_activation_hook(__FILE__, array(‘Recipress’,’Install’));
    • Step 4: Initialize and Register the post type ‘recipe’· Add an Initialize function to your class.· Register the post type in this function. function Initialize() { ... register_post_type(RECIPRESS_POST_TYPE,$args); ... }· Outside of our class we will call Initialize to hook into ‘init’add_action(‘init’, array(‘Recipress’, ‘Initialize’));
    • Register the Taxonomy (Category)· We can use the built-in taxonomy for categories, but we wont.· We’ll create a custom taxonomy so that it’s specific to Recipes.· Add the below code to the end of the Initialize function. register_taxonomy( ‘cat’, array(RECIPRESS_POST_TYPE), array(‘hierarchical’ => true, ‘show_ui’ => true, ‘query_var’ => true, ‘rewrite’ => array( ‘slug’ => ‘category’ ), ));
    • Step 5: Showing Categories in column layout· Adding a new column to your custom posts list in the Admin. function edit_column_title($defaults) { $defaults[‘cat’] = ‘Category’; return $defaults; } function edit_column_value($column_name, $post_id) { $taxonomy = $column_name; ... echo join( ‘, ‘, $post_terms ); }· Outside of our class we will call a filter and action to add info.add_filter( ‘manage_recipe_posts_columns’, array(‘Recipress’,’edit_column_title’) );add_action(‘manage_recipe_posts_custom_column’, array(‘Recipress’,’edit_column_value’), 10, 2);
    • Step 6: Add a Custom Meta Box· A custom post type needs a custom meta box :-)· Utilize any type of form element· We’ll setup a textbox forCalorie Count in the sidebar. function meta_options(){ global $post; $custom = get_post_custom($post->ID); $calories = $custom[“calories”][0]; ?> <label>Calories:</label><input name=”calories” value=”<?php echo$calories; ?>” /> <?php }
    • Front End Display· Now that we have the admin created, how do we display what we created?Displaying a Single Custom Post· Create single-recipe.php to make a single post page.Looping Through your Custom Posts· Place this snippet of code right before the loop.<?php query_posts( ‘post_type=recipe’); ?>Display custom meta box content· Do it the same way in the meta_options function.$custom = get_post_custom($post->ID);$calories = $custom[“calories”][0];echo $calories;
    • Recap· Custom Post Types are new for WP3.· Creative control over custom content.· Easier on developers; no more relying on custom fields.· Easier for your clients to use; have their own navigation link.· Great for things that can be cataloged, but not for categorizing· We walked through creating a plugin that allows a user to enter Recipes · We registered our post type of ‘recipe’ · We registered our taxonomy ‘cat’ · We added a custom meta box · Then we displayed the content for the user.
    • Resources· http://codex.wordpress.org/Function_Reference/register_post_type· http://codex.wordpress.org/Taxonomies· http://thinkvitamin.com/code/create-your-first-wordpress-custom-post-type/
    • Thank You. · Questions?