Registering your own Custom Post Types

15
Aug

After using WordPress for several years now, I still love the aspect and ease of being able to create my very own custom post types.

Custom Post Types were first introduced to WordPress in version 3.0 and are very simple to create and use. Having the ability to create new post types transformed WordPress from a blogging platform to a full blown Content Management System.

A lot of WordPress theme developers create and add custom post types to themes as a way to add a new “Product” or “Category” types to your site.

With this in mind I am going to show you how simple it is to create or register your own Custom Post Types.

Registering your Custom Post Types

For demonstration purposes we are going to create a recipe custom post type.

To create or register a post type we use the WordPress register_post_type()  function

1
2
3
<?php
register_post_type( $post_type, $args );
?>
<?php
register_post_type( $post_type, $args );
?>

The first thing we need to do is register our new recipe post type. Navigate to your theme folder “wp-content/themes/my-current-theme” and open up the file called functions.php.

In this file add the below code:

1
2
3
4
5
6
7
8
9
10
11
12
$args = array(
'label' => __('Recipes'),
'singular_label' => __('Recipe'),
'public' => true,
'show_ui' => true,
'capability_type' => 'page',
'hierarchical' => false,
'rewrite' => true,
'query_var' => 'recipes',
'supports' => array('title', 'thumbnail')
);
register_post_type( 'recipe' , $args );
$args = array(
'label' => __('Recipes'),
'singular_label' => __('Recipe'),
'public' => true,
'show_ui' => true,
'capability_type' => 'page',
'hierarchical' => false,
'rewrite' => true,
'query_var' => 'recipes',
'supports' => array('title', 'thumbnail')
);
register_post_type( 'recipe' , $args );

It a nutshell this short array has registered out new recipes post type. Easy! It also adds a new menu item in the WordPress Dashboard called “Recipes“.

In a later article I will demonstrate how simple it is to use these newly registered custom post types within the front of your site.

Enjoy this article? If so, we would love to hear your thoughts in the comments below

 

Related Blog Posts

Leave a Reply