Custom Post Types with Custom Categories

27 Shares
Custom Post Types with Custom Categories
27 Flares Twitter 23 Facebook 0 StumbleUpon 1 Google+ 1 LinkedIn 2 Buffer 0 Reddit 0 Pin It Share 0 Email -- 27 Flares ×

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.