By default, WordPress has two type of content, post, and pages. The post is further organized based on categories and tags while pages are organized in parent-child relationships. Custom post type is a new type of post with its own name, it’s own selection in the admin menu and it’s own template taxonomies and functions. The benefit of the custom post type is simply that it is separate from your regular posts and allows you to put a different type of content in different pockets. Custom post type can be added to a plugin or hardcoded into the theme you are using. In our case, we will hardcode custom post type in our custom WordPress theme.
Setting up custom post type is easy and only require few lines of code in function.php
. You can add anything into your WordPress theme using custom post type like, testimonials, videos, posts, listings of all kind but in our case, i’m going to add “Sponsors” custom post type.
Register Custom Post Type (Step 1)
Add the following code in function.php
to register a custom post type in WordPress admin panel.
<?php // Add new post type for Company Sponsors add_action('init', 'company_sponsors_init'); function company_sponsors_init() { $args = array( 'label' => _x('Sponsors', 'post type general name'), 'singular_label' => _x('Sponsors', 'post type general name'), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title','editor','author','thumbnail','excerpt','comments','custom-fields'), 'has_archive' => 'recipes' ); register_post_type('sponsors',$args); } ?>
In this above code company_sponsors_init( )
is the function that creates the actual custom post type which contains several different arguments and register_post_type
is the function used to register this custom post type.
Now different arguments are for different purposes and we will now discuss what exactly the arguments are doing in our function.
label
is the argument where we add all the labels for custom post type using an array but for now I’m just adding a name of the custom post type which is displayed as it is within the admin panel.singular_label
is for singular instances.public
make custom post type publicly visible.show_ui
shows the UI where a user can add a title and description.query_var
allow you to query the content to display your required information i.e. title, content, excerpt etc.rewrite
is set to true which allow WordPress to rewrite the URL. We will get back to that later.capability_type
can be set to post or page, for I’ve set it to post.hierarchical
is set to false for this tutorial.menu_position
is set to null.support
tells WordPress what content is supported by the actual individual posts.
Customize Labels for Custom Post Type (step 2)
After the custom post type is registered you will see that the labels displayed inside WordPress admin still calls all of them as Post which is not a professional approach. we can fix out this with a few more lines of code in our function.
Create a new array $sponsor_labels
and paste it directly above the first array $args
then call it from our basis function by replacing label with $sponsor_labels
and removing argument singular_name
because it is already repeated in our new array
// Add new post type for Recipes add_action('init', 'company_sponsors_init'); function company_sponsors_init() { $sponsor_labels = array( 'name' => _x('Sponsors', 'post type general name'), 'singular_name' => _x('Sponsors', 'post type singular name'), 'all_items' => __('All Sponsors'), 'add_new' => _x('Add New Sponsors', 'sponsors'), 'add_new_item' => __('Add New Sponsor'), 'edit_item' => __('Edit Sponsor'), 'new_item' => __('New Sponsor'), 'view_item' => __('View Sponsors'), 'search_items' => __('Search in sponsors'), 'not_found' => __('No sponsors found'), 'not_found_in_trash' => __('No sponsors found in trash'), 'parent_item_colon' => '' ); $args = array( 'label' => $sponsor_labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title','editor','author','thumbnail','excerpt','comments','custom-fields'), 'has_archive' => 'recipes' ); register_post_type('sponsors',$args); }
If we look at the array $sponsor_labels
you will see we have a more specific definition of all the labels in our custom post type.
Changing menu position of custom post type
In array: 'menu_position' => 5,
What the values mean:
- 0 – at the very top
- 5 – below Posts
- 10 – below Media
- 15 – below Links
- 20 – below Pages
- 25 – below comments
- 60 – below first separator
- 65 – below Plugins
- 70 – below Users
- 75 – below Tools
- 80 – below Settings
- 100 – below second separator
Conclusion
It is very easy to add Custom Post type to your WordPress theme. Custom post type help to develop a more user-friendly theme. This tutorial explains how you can set up basic custom post type, add advance variables and changing menu position.