Custom post types can be used in your WordPress setup to create customised content similar to regular posts but with some differences. With a few lines of PHP code it is very easy to create a custom post type without the use of a plugin as I will show you in this tutorial.
- Open your theme’s Functions.php file:
Along the side of the WordPress dashboard options, hover to ‘Appearance’ then click on ‘Editor’. Once in the Theme Editor, find Theme Functions (functions.php) file in the list on the right of your screen and click on it to open it. - Create a new function for your custom post type:
Enter the following sample of code into your functions.php file, changing the name of the function (my_custom_posttype) to any other name if you wish. But do not yet update the file. - Register the post type:
Enter the following code sample (underlined) between the { } brackets: - include add_action command for post type function:
Enter the following code sample (underlined) after the closing } bracket for the function: - Click Update File
- Your custom post type should display on the left-hand side menu in your Dashboard view
function my_custom_posttype() {
}
b. ‘singular_name’ – the name for one incidence of the custom post type (for example singular of pages is page, posts is post and events is event etc).
function my_custom_posttype() {
register_post_type( ‘event’,
array(
‘labels’ => array(
‘name’ => __( ‘Events’ ),
‘singular_name’ => __( ‘Event’ )
),
‘has_archive’ => true,
‘supports’ => array(‘title’, ‘editor’, ‘thumbnail’),
‘taxonomies’ => array( ‘category’ ),
) );
}
As an example, here I am creating an ‘Event’ custom post type but this could easily be for a Product, Profile, Review, Story or anything you can imagine. The basic code is the same regardless.
You can change the following parameters under ‘Labels’. Other parameters in the function are optional but recommended:
b. ‘supports’ – add any attributes that you would like this custom post type to support.
c. ‘taxonomies’ – add any taxonomies (such as ‘category’) that you would like the post type to support.
function my_custom_posttype() {
register_post_type( ‘event’,
array(
‘labels’ => array(
‘name’ => __( ‘Events’ ),
‘singular_name’ => __( ‘Event’ )
),
‘has_archive’ => true,
‘supports’ => array(‘title’, ‘editor’, ‘thumbnail’),
‘taxonomies’ => array( ‘category’ ),
) );
}
add_action( ‘init’, ‘my_custom_posttype’ );
If you changed the name of the function earlier, you should use the same function name in the add_action command.
The complete sample code is as follows:
function my_custom_posttype() {
register_post_type( ‘event’,
array(
‘labels’ => array(
‘name’ => __( ‘Events’ ),
‘singular_name’ => __( ‘Event’ )
),
‘public’ => true,
‘has_archive’ => true,
‘supports’ => array(‘title’, ‘editor’, ‘thumbnail’),
‘rewrite’ => array(‘slug’ => ‘event’),
‘taxonomies’ => array( ‘category’ ),
) );
}
add_action( ‘init’, ‘my_custom_posttype’ );
If you wish to download the sample code, click here
This was intended as a short tutorial but if you have any further questions, comments, feedback or queries please leave them below.