Custom Post Types in WordPress 3

Author: Lee Aplin

With the addition of custom post types, WordPress 3 can finally hold its own against its rivals in the CMS market. I’ve previously used custom fields a whole lot during projects to separate content and allow clients to manage this content a bit better, but thanks to custom post types we are now able to make adding content to a site a really simple process and the client no longer has to worry about which custom field they should be editing.

While I wouldn’t say custom post types are an ideal way of labelling blog posts (this is what categories and taxonomies are for) they are ideal for giving you much more control over what content you can edit in a post.

Set up custom post types

Setting up the custom post type couldnt have been easier. Crack open the functions.php file in your WordPress theme and add the following code:

add_action('init', 'work');
function work() {
$args = array(
'label' => __('Work'),
'singular_label' => __('Work'),
'public' => true,
'show_ui' => true,
'menu_position' => 5,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'supports' => array('title', 'editor', 'thumbnail', 'page-attributes', 'excerpt')
);
register_post_type( 'work' , $args );
}

This is a pretty bloated example of adding a custom post type but there still isn’t too much to take in. We have added the post type named “Work” and an array of arguments. There are lots of arguments added in this example but you can set up your custom post type with as many or as few as you like (view the full list of arguments at the WordPress Codex)

At this point you have just created your new custom post type as you can see below. This is its most basic form allowing you to create posts with a title field and a content area.

Display your single posts

Next you will want to display the singular view of your custom post type on your website. The best way to do this is to create a new template in your theme. WordPress will usually use single.php to display your posts as default. To add a custom template in its most simple form, duplicate single.php and rename it with the name of your custom post type after the word single. For example, in my theme the custom post type is called work, so the name of the template is single-work.php. You can create a different template for every custom post type you create.

Its just as easy to list these posts in any of your templates using a custom query such as in this basic example below:

<?php
query_posts("post_type=work");
while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink () ;?>"><?php the_title(); ?></a>
    <div><?php the_excerpt(); ?></div>
<?php endwhile; ?>

Expand upon the custom post types

The name says it all really, CUSTOM post types. So surely you will want them to be customised and different to standard posts. This is where we can get creative and really polish the user experience.

In the example below I created several text fields and text areas in the Work custom post type. I wanted to allow to client to easily add job titles, client names, a box which would display client feedback and a section where they could embed Vimeo videos without having to fiddle about with custom fields.

To add a new text field into the backend of your custom post type modify the following code to suit your needs:

<?php
add_action("admin_init", "admin_init");
add_action('save_post', 'save_custom');

function admin_init(){
add_meta_box("workInfo-meta", "Thumbnail", "meta_options", "work", "side", "low");
}

/* Thumbnail Field */

function meta_options() {
global $post;
$custom = get_post_custom($post->ID);
$thumb = $custom["skt_thumb"][0]; ?>
<input name="thumb" value="<?php echo $thumb; ?>" /> <?php
}

function save_custom() {
global $post;
update_post_meta($post->ID, "skt_thumb", $_POST["thumb"]);
}
?>

In the above example we have added a meta box into the admin area with a title of Thumbnail, positioned in the right hand side of the page in the backend. This custom field has a key of skt_thumb. For a full description of all of the parameters used in this string take a look at the WordPress Codex page

Using a standard get_meta_post we can call the data from theses custom fields into our template and display them beautifully!

There really is so much more to custom post types than what I have sketchily described here. They have opened up a whole new way of handling content in posts and hopefully you will play around with them and find loads of interesting ways to make WordPress work better for you and your clients. At Substrakt we’re loving them and looking forward to putting them into practice in our new projects.