Easiest Ways To Create Custom Post Types Like an EXPERT- Practical Guide

DECEMBER 30, 2016  8 MINS READ

Custom post types are the new post types that you add to the WordPress. WordPress can hold and maintain many different types of content. POST itself post types of WordPress, the default post types are Post, Page, Attachment and navigation.

custom-post-types


With the default post types, you can also create custom post types to the WordPress, in this post we are going to see how to create custom post type in WordPress using plugin and manually

[Tweet “Easiest Ways To Create #CustomPostTypes Like an EXPERT- Practical Guide #wpts”]

Creating custom post types using plugin

The easiest way to create custom post types is by using the plugin. The most commonly used plugin out there to help you is Custom Post Type(CPT). Install the plugin from the WordPress plugin directory.

[recommened_reading id=”1047″ title=”Recommended Reading:”]

After installing there will be a new option in the admin area CPT UI. Now select CPT UI> Add/Edit post types to create custom post types. You also have options to Add/Edit taxonomies, a subcategory or tags to the custom post types you are creating

Under the Add/edit post types, you have the options for basic settings, Additional labels and other advanced options based on your needs. The common field used are

creating custom post types using plugin

Post type slug- slug for the custom post type you are creating now.
Plural label- Label for the post type like Movies, Recipes…
Singular Label- Singular form the custom post types name
Post type description- add the post type description to give a short note about the custom post types.

Now click the Add post type, that’s all the custom post types you needed are created to your WordPress website.

[exclusive_bonus title=”Reader’s Benefit:”]Value added benefit to WP Team Support readers. Free PDF download Easiest Ways To Create Custom Post Types Like an EXPERT- Practical Guide.[/exclusive_bonus]

Creating Custom post types manually

Creating a custom post type manually is quite a tedious job. You have to get into the field yourself and make the website look the way you want. But need not to worry we are here to help you make it easier. Let us start to create a custom post type for your website.

We are here to help you make it easier. Let us start to create a custom post type for your website https://t.co/5bK7D1zXnLpic.twitter.com/nGXcQMOfhn

— WPTeamSupport (@wpteamsupportHQ) April 4, 2017

Creating function.php for the custom post types

One of the easiest ways to in manual method is to use the register_post_type()function. We have to make use of the functions.php file in the theme file of your website like we have done in custom 404 page. Use the following code in the theme file.

[Tweet “Code for creating a #customposttypes manually for #WordPress websites #wpts”]

[code lang=”html”]function portfolios_init() {
$args = array(
‘label’ => ‘Portfolios’,
‘public’ => true,
‘show_ui’ => true,
‘capability_type’ => ‘post’,
‘hierarchical’ => true,
‘rewrite’ => array(‘slug’ => ‘portfolios’),
‘query_var’ => true,
‘menu_icon’ => ‘fa-address-card’,
‘supports’ => array( ‘title’, ‘editor’, ‘excerpt’, ‘trackbacks’, ‘custom-fields’, ‘comments’, ‘revisions’, ‘thumbnail’, ‘author’, ‘page-attributes’,)
);
register_post_type( ‘portfolios, $args );
}
add_action( ‘init’, ‘portfolios_init’ );
[/code]

In the above code, we have mentioned some commonly used functions and labels. As like in the CPT plugin you got the options like

Label name- here we mentioned as Portfolios.
Post type slug- slug for the custom post type you are creating now.
Hierarchical- to rank the page according to the priority or the precedence.

[experts_box experts_title=”Easy One Click Solution:”]Customize your WordPress website give your own personal touch. Buy the Design and Customization Support Service[/experts_box]

Dissecting the code

Dissecting the #WordPress #custom #post-type for better understanding https://t.co/5bK7D1zXnLpic.twitter.com/XGNhoHkgbl

— WPTeamSupport (@wpteamsupportHQ) April 4, 2017


To make it more clear for you, let’s dissect the above code for better understanding

[table][thead][tr][th]Coding[/th][th]Explanation[/th][/tr][/thead][tbody][tr][td]function portfolios_init()[/td][td]we are creating a function for our custom post types to have its own functions and limit it from conflicting from other codes in the functions.php file[/td][/tr][tr][td]‘label’ => ‘Portfolios’[/td][td]label is the name we are giving to the custom post types. We are naming it as Portfolios now.[/td][/tr][tr][td]‘public’ => true[/td][td]here we are making the function to public so that it can be accessed by the front end users and from the admin panel too.[/td][/tr][tr][td]‘show_ui’[/td][td]it creates a default user interface to manage the custom post types created from the admin panel.[/td][/tr][tr][td]‘capability_type’ => ‘post’[/td][td]here we are mentioning that our new custom post types are of the WordPress default post types.[/td][/tr][tr][td]‘hierarchical’ => true[/td][td]To rank the page according to the priority or the precedence, we will deal with this later.[/td][/tr][tr][td]‘rewrite’ => array(‘slug’ => ‘portfolios’)[/td][td]we are assigning the slug to the custom post types we are creating here it is portfolios.[/td][/tr][tr][td]query_var[/td][td]It sets the post type name as a query variable.[/td][/tr][tr][td]‘supports’ => array()[/td][td]this defines the features the custom post types are going to have. These features will be handled in the admin area.[/td][/tr][tr][td]register_post_type( ‘portfolios, $args )[/td][td]this is the method we are following here, we are registering our custom post types functions.[/td][/tr][/tbody][/table]

So now we have register our functions.php file of custom post type- Portfolios. When you return to the admin panel you can see the custom post type Portfolios on the left-hand side. By clicking Add New you can start creating the custom post types in the Portfolios like you write the blog post.

Custom Taxonomy

Now you have the main post type Portfolios for your custom post types, we can also add subcategory or subdivisions or tags to this main custom post type to keep it more organized. It is known as taxonomy.

[Tweet “Creating #customtaxonomy for #WordPress websites #wpts”]

We are going to create custom taxonomy as like we have created the custom post types via register_taxonomy() function. Use the following code in the functions.php file

[code lang=”html”]function taxonomy_portfolios_photography_init() {
$args = array(
‘label’ => ‘Photography’,
‘public’ => true,
‘show_ui’ => true,
‘capability_type’ => ‘post’,
‘hierarchical’ => true,
‘rewrite’ => array(‘slug’ => ‘portfolios_photography’),
‘query_var’ => true,
‘menu_icon’ => ‘ fa-thumb-tack ‘,
‘supports’ => array( ‘title’, ‘editor’, ‘excerpt’, ‘trackbacks’, ‘custom-fields’, ‘comments’, ‘revisions’, ‘thumbnail’, ‘author’, ‘page-attributes’,)
);
register_taxonomy( ‘taxonomy_portfolios_photography’, $args );
}
add_action( ‘init’, ‘taxonomy_portfolios_photography_init’ );
[/code]

Making the custom post types visible in the front end

After registering the custom post types in your WordPress website you can start adding the posts to your website. To make the custom post types publicly visible to the front end user we need to create the navigation link to the custom post types we created here ‘Portfolio’.

Select the Appearance tab in the admin panel, head on to the menus tab, add the custom post types we created ‘Portfolio’. Now the portfolio pages will be visible for the front end users.

WordPress Hierarchy

wordpress template hierarchy


WordPress template hierarchy shows how the WordPress will search for the pages in the WordPress websites and based on which the pages will be displayed.

On comparing this template hierarchy with our coding

First, the WordPress looks for the Portfolio page and looks for its template php file and displays the result.

on-comparing

On getting the Portfolio page, since we gave hierarchy as true and we didn’t give any particular template file WordPress take default index.php file template as the Portfolios PHP file and display it as like it displays the default post type.

[recommened_reading id=”1037″ title=”Recommended Reading:”]

If you have created a separate template file for the portfolio like portfolio.php file it takes that file and displays the content based on that portfolio.php file.

This is what happen with the taxonomy files also, the taxonomy pages will also be displayed as a default WordPress post type pages.

Anchors Aweigh

You are all clear to create a custom post types. Custom post types can be created in many ways the ways mentioned above are one of the easiest methods.

It takes a little bit of time to understand what actually going on with this code, to make the things even more simpler for you there are custom post types generators. It generates the code required for you.

[Tweet “Reliable #WordPress #customposttype generators you can make use of”]

[pro_tip title=”Pro Tip:”]Custom Post type generators

  • Generate wp
  • Foo plugin [/pro_tip]

    Generate wp is well known and reliable source which has all the basic codes required for WordPress. You can update the code to the way you want and just copy and paste it in your functions.php file.

    Foo plugin made it more simpler for you just enter the required field you want and the required code is be generated for you.

    If you have some more easy ways share with us and other readers in the comment section, let us make the WordPress website the way you want.

    Leave a Reply

    Your email address will not be published. Required fields are marked *