Custom Post Types & TaxonomiesGregg Henry
triple 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 Types
Custom 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 name
define(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 rules
define(RECIPRESS_POST_TYPE, ‘recipe’);class Recipress{
function Install() { flush_rewrite_rules(); }
} //end Recipress Class
register_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 for
Calorie 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?
Top Related