How to Make a Custom Post Type in WordPress

Today we’re going to talk a bit about custom post types in WordPress, and how they can further help to develop a unique layout for your website. There are plenty of reasons that a blog owner would have to make use of custom post types.

For instance, a photographer can use the build in gallery shortcode to easily create collections of images from a particular set. This can allow easy updating of a portfolio in a news style format, with newer work remaining at the top of the page of posts. For full page viewing of photos, or to avoid overflow, they may want to remove sidebars or asides from the main content of the site. This can be seen on the photography theme, where we’ve made a custom type of gallery to display your pictures.

Awesome, how do I do it?

To use this feature of WordPress, you simply add a bit of code to your functions.php file, like so:

add_action( 'init', 'create_post_type' );
function create_post_type() {
	register_post_type( 'custom_post',
		array(
			'labels' => array(
				'name' => __( 'Custom Posts' ),
				'singular_name' => __( 'Custom Post' )
			),
		'public' => true,
		'has_archive' => true,
		)
	);
}

What this has done is created a type of post known as “custom_post”. The name of the type will show up as “Custom Post” if there is only one of that type to list, and “Custom Posts” if there are multiple. The “public” argument allows it to be viewed as a separate pane on the admin dashboard, and “has_archive” allows it to show up if searched for on the website. These two are both declared in the example because their default is false, and it wouldn’t be very useful for you to make a custom post type and have yourself and visitors of your website be unable to access it, would it?

For more information on parameters that can be defined, check out the register_post_type function page on the WordPress.org Codex. I’m not going to venture too far into this, as it’s mostly things to add detail beyond the new type that you may not need, but feel free to explore further. In the meantime, let’s discuss how this new custom post type that we’ve created can be used.

What can I do with this?

As I’d mentioned earlier, defining the “public” argument as true allows you to see a separate panel in your wordpress dashboard. This will allow one easy area to make changes to posts that fit in that type, without other posts getting in the way. That’ll make it look something like this:
Custom Post Type WordPress

The name that we chose, “custom_post”, works fine, but we will want to make it more original than that for regular use. While the display name can be whatever we choose, it’s best to avoid the possibility of it clashing with additional themes or plugins that we may use, rather than trying to troubleshoot later. This would be done with a namespace. A namespace is a prefix that can be added to ID an action or an element. Provided that it’s under 20 characters in length, almost anything with standard characters can go here, though I’d suggest staying away from “wp_” as your namespace, as it is being adopted as the core standard for later versions of WordPress. Custom post types that I would create for this blog, for instance, would take the name “david_custom_post”, with “david” being the namespace. Better to work smart and not hard, so define these from the start, and you won’t have to worry about forgetting and having your site break due to a conflict with some add-on down the road.

How does this show up to users on the site?

Sticking with our example of “david_custom_post”, we’ll create a post titled “Some Content”. If you are using %postname% as your slug, this would show up as http://example.com/david_custom_post/some-content/ That’s fine and dandy, and it sticks with our custom namespace guidelines, but it’s not quite as clean, is it? Instead, we want it to show up with a slug of “custom_post”. No problem,ย  just add a rewrite!

add_action( 'init', 'create_post_type' );
function create_post_type() {
	register_post_type( 'custom_post',
		array(
			'labels' => array(
				'name' => __( 'Custom Posts' ),
				'singular_name' => __( 'Custom Post' )
			),
			'public' => true,
			'has_archive' => true,
			'rewrite' => array('slug' => 'custom_post')
		)
	);
}

That extra argument that we applied declares the slug with a value of “custom_post”, which again, can be most anything that you choose. Now when people want to view a post, the url that they will use will be http://example.com/custom_post/some-content/. Again though, you need to be careful with what you choose as a slug, as it could interact with other plugins that you may use. For instance, many shopping carts use “products” as their slug, having your online storefront located at http://example.com/products/. For this reason, as long as it’s descriptive and fits within your SEO plan, choose something a bit more unique. Better safe than sorry!

I’ve got the post type, now how to lay them out differently?

As of wordpress 3.1, there is support for both single and archive templates. If you are working this far in, I’ll venture that you can alter the layout of a single post view via the single.php template file of your theme. Now, you can create template files called single-david_custom_posts.php and archive-david_custom_posts.php, and edit them to style how posts within that specific type will be viewed. As stated at the beginning of this article, there are plenty of uses for custom post types, depending on the needs of your blog or website.

All done, now what?

That’s it! We’ve created our own post type, added it to the admin dashboard, created a unique slug for it to be indexed as, and added a template for how the post should display to someone visiting the website. While there’s always room to make additions, feel free to wow your visitors with unique layouts within your theme depending on what content they view.

Questions? Comments? Let me know what you do with this new-found ability, and feel free to share examples in the comments!


Posted

in

,