
Custom Post Types with Custom Categories
WordPress is becoming such an indispensable platform to so many companies, but the average company doesn’t even take advantage of a fraction of the capabilities. One of our clients wanted to add a resource section to their site but didn’t want to do it using pages nor in blog posts. This is exactly what WordPress supports Custom Post Types for!
In this case, we wanted to add a Resource Section to one of our clients’ sites. It’s fairly simple to add a Custom Post Type to your WordPress theme. You add the following code using the function register_post_type to your functions.php page:
// Add Resources Custom Post Type add_action( 'init', 'create_post_type' ); function create_post_type() { register_post_type( 'resources', array( 'labels' => array( 'name' => __( 'Resources' ), 'singular_name' => __( 'Resource' ), 'add_new' => __( 'Add New' ), 'add_new_item' => __( 'Add New Resource' ), 'edit_item' => __( 'Edit Resource' ), 'new_item' => __( 'New Resource' ), 'all_items' => __( 'All Resources' ), 'view_item' => __( 'View Resource' ), 'search_items' => __( 'Search Resources' ), 'not_found' => __( 'Resource Not Found' ), 'not_found_in_trash' => __( 'No Resources in Trash' ), 'parent_item_colon' => '', 'menu_name' => __( 'Resources' ) ), 'public' => true, 'has_archive' => true, 'rewrite' => array('slug' => 'resources'), 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) ) ); }
A little more difficult to find was how to make custom categories for your Custom Post Type. One reason why it’s difficult to figure out how to do this is because it’s called a custom taxonomy and utilizes the register_taxonomy function to customize it. In this case, we’re wanting to add resource types like Webinars, Whitepapers, etc. to the theme… so here’s some additional code for the functions.php file:
add_action( 'init', 'resource_category_init', 100 ); // 100 so the post type has been registered function resource_category_init() { register_taxonomy( 'type', 'resources', array( 'labels' => array( 'name' => 'Resource Type', 'singular_name' => 'Resource Type', 'search_items' => 'Search Resource Types', 'popular_items' => 'Popular Resource Types', 'all_items' => 'All Resource Types', 'edit_item' => __( 'Edit Resource Type' ), 'update_item' => __( 'Update Resource Type' ), 'add_new_item' => __( 'Add New Resource Type' ), 'new_item_name' => __( 'New Resource Type' ) ), 'hierarchical' => 'false', 'label' => 'Resource Type' ) ); }
Custom Post Types also allow you to design the archive and single pages for your Custom Post Types. Just copy the archive.php and single.php files. Rename the copies with the Custom Post Type in the name. In this case, that would be archive-resources.php and single-resources.php. Now you can customize those pages however you wish the resource page to look.
Another way would be to use a plugin like Easy Custom Content Types or Types.
These plugins also enable you to easily add custom meta boxes and create custom page and post templates.
Very true @google-d5279c8b66d25549a0ec3c8dd46a3d1a:disqus ! I’ll be honest that I feel like a couple of the plugins add a ton of overhead to the blog… and you can’t just move a theme from one site to the next, you have to make sure you move the plugins, too. It’s the only reason I work to embed the necessary functions in the theme files.