Content Marketing

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.

Douglas Karr

Douglas Karr is the founder of the Martech Zone and a recognized expert on digital transformation. Douglas has helped start several successful MarTech startups, has assisted in the due diligence of over $5 bil in Martech acquisitions and investments, and continues to launch his own platforms and services. He's a co-founder of Highbridge, a digital transformation consulting firm. Douglas is also a published author of a Dummie's guide and a business leadership book.

Related Articles

2 Comments

  1. 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.

    1. 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.

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close

Adblock Detected

Martech Zone is able to provide you this content at no cost because we monetize our site through ad revenue, affiliate links, and sponsorships. We would appreciate if you would remove your ad blocker as you view our site.