Custom Post Types in WP3

20
Custom Post Types & Taxonomies Gregg Henry triple G interactive @GreggHenry

Transcript of Custom Post Types in WP3

Page 1: Custom Post Types in WP3

Custom Post Types & TaxonomiesGregg Henry

triple G interactive

@GreggHenry

Page 2: Custom Post Types in WP3

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.

Page 3: Custom Post Types in WP3

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!

Page 4: Custom Post Types in WP3

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

Page 5: Custom Post Types in WP3

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?

Page 6: Custom Post Types in WP3

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/*/

Page 7: Custom Post Types in WP3

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{

}

Page 8: Custom Post Types in WP3

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’));

Page 9: Custom Post Types in WP3

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’));

Page 10: Custom Post Types in WP3
Page 11: Custom Post Types in WP3

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’ ), ));

Page 12: Custom Post Types in WP3
Page 13: Custom Post Types in WP3

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);

Page 14: Custom Post Types in WP3
Page 15: Custom Post Types in WP3

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 }

Page 16: Custom Post Types in WP3
Page 17: Custom Post Types in WP3

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;

Page 18: Custom Post Types in WP3

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.

Page 19: Custom Post Types in WP3

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/

Page 20: Custom Post Types in WP3

Thank You.

· Questions?