WordPress Custom Post Type and Meta Boxes Example
WordPress 3.0 is coming soon and one of the biggest new features is the ability to add custom post types. Making these custom post types is pretty easy, below is a simple but complete example of creating a link type custom post.
Register the Custom Post
The first thing you see here is the
register_post_type function which starts everything. You can read about this on the codex page or Justin Tadlock's post. These two pages should get you up to speed fast.
Actions and Filters
The next bit of code are all the callbacks. The first two deal with adding the meta box and saving it. The next two change the columns in the WordPress admin when viewing a list of the custom posts. The last is for using custom template names.
The Meta Box
The first two functions
save_post_data() deal with the added meta box. I’ve kept things simple here so it should be easy to follow what’s happening.
When looking through the list of your custom post you’ll probably want to display custom columns so it’s easier to find what you want. The
custom_nav_columns control this:
nav_columns sets everything up, and
custom_nav_columns determines what will be listed there.
Custom Template Names
Wordpress by default will use ”single-link.php” for our template name since we named the custom post type ”link”. But if you wanted to use a different template name for instance ”foorbarlink.php” you can change it using this part of the code. If you’re happy with WordPress’ default naming you can exclude this completely. Below is the function for a link type post, one that you might use in a tumble log. It's a basic example of how to use the Custom Post Type feature in WordPress 3.0+