Creating WordPress Custom Post Type & Templates


Blog 5

WordPress allows you to make all types of customisations. One benefit of all these customizations is that you can create your own custom post types.

For those of you who don’t know what custom post types can do, here is what you should know.

  • Custom post types turn your website blogging panel into a complete content management system
  • You can create more content types except for pages and blogs. If you want a custom post type for infographics, then that is possible too
  • Your custom post type can have its own custom fields and a completely customized custom category structure
  • Custom post types offer many more opportunities to the users such as creating their own deals, glossary, or rating pages

Now that you have a fair idea about what custom post types do, let’s learn how to create your own custom post types.

What are Custom Post Types?

Custom post types are content pages that allow you to create types other than pages and posts. These give you more leverage and control over what type of content you want to upload.

The default WordPress custom post types include:

  • Posts
  • Pages
  • Attachments
  • Revisions
  • Nav Menu

You can create pages for your website content and posts for your blog. If you need a post type for a separate content design, then you would need a custom post type.

Here is how to design them.

Method 1: Use a Plugin to Create Custom Post Types

By far the easiest way to create a custom post type in your WordPress website is by using a plugin.

You can download the Custom Post Type UI plugin from the WordPress plugin menu. Once the plugin is added to your website, simply go to CPT UI > Add New.

Custom Post Type UI

Need Custom Post Types? Get Help from Professional WordPress Experts. Book a Call Today!

It will ask you for the labels. Add them as necessary. Make sure to add a slug, a singular label, and then scroll down.

The plugin will ask you what editing features you will need for the custom post type. Select as many as you want to be added to the CPT.

Support CPT

Add attributes, descriptions, and taxonomies for your custom post type. The plugin gives you multiple options to choose from about the functionality of the CPT you just created.

CPT settings

Once you have changed all the settings, the next step is to save the custom post type. So click on ‘Add Post Type’ to save the settings. Congrats! You have just created your very first Custom Post Type in WordPress.

Method 2: Create Custom Post Types in WordPress Manually

The problem with the custom post types you create using a plugin is that they automatically disappear as soon as the plugin is removed from the website or disabled.

So, the best way to create custom post types is to do it manually. You will need access to the functions.php file.

/* Custom Post Type Start */
 
function create_posttype() {
register_post_type( 'news',
// CPT Options
 
array(
  'labels' => array(
   'name' => __( 'news' ),
   'singular_name' => __( 'News' )
  ),
  'public' => true,
  'has_archive' => false,
  'rewrite' => array('slug' => 'news'),
 )
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
 
/* Custom Post Type End */

Here is the code that you need to add to your functions.php file in WordPress. Once you add the following code, you will see ‘News’ being shown on your WordPress website.

News CPT

Now, you will add description and more details to the custom post type to give it more value.

Tired of manually creating WordPress Customer Types. Our WordPress Solutions can get the job done. Book a Call Today!

/*Custom Post type start*/
 
function cw_post_type_news() {
 
$supports = array(
'title', // post title
'editor', // post content
'author', // post author
'thumbnail', // featured images
'excerpt', // post excerpt
'custom-fields', // custom fields
'comments', // post comments
'revisions', // post revisions
'post-formats', // post formats
);
 
$labels = array(
'name' => _x('news', 'plural'),
'singular_name' => _x('news', 'singular'),
'menu_name' => _x('news', 'admin menu'),
'name_admin_bar' => _x('news', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New news'),
'new_item' => __('New news'),
'edit_item' => __('Edit news'),
'view_item' => __('View news'),
'all_items' => __('All news'),
'search_items' => __('Search news'),
'not_found' => __('No news found.'),
);
 
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'news'),
'has_archive' => true,
'hierarchical' => false,
);
register_post_type('news', $args);
}
add_action('init', 'cw_post_type_news');
 
/*Custom Post type end*/

The following code tells WordPress that the custom post type is compatible and supports all essential features.

Now you will need a template for the new custom post type you just created to show it as a template on posts and pages in ‘Page Attribution’ column.

<?php
/*Template Name: News*/
get_header();
query_posts(array(
   'post_type' => 'news'
)); ?>
<?php
while (have_posts()) : the_post(); ?>
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?></p>
<?php endwhile;
get_footer();
?>

Save this content to ‘template-news.php’ or any other name that you can remember for the custom post type you just created.

create post

This way you can change from post or page to ‘news’ custom post type we just created. If you want to

page template

Now it is time to create a view for the custom post type you just created. For this, add the following code to ‘single-news.php’

<?php
get_header();
/* Start the Loop */
while (have_posts()) : the_post();
   get_template_part('template-parts/post/content', get_post_format());
endwhile; // End of the loop.
get_footer();
?>

The following code will display the custom post type as a single page on your website.

That’s it. Your custom post type is ready for use. We just added a simple custom post type. If you want to make any changes, simply add them to the code we created above. You can customize the code on Notepad++ or Sublime editor.

Bottom-line

Creating custom post types in WordPress is not so easy and not everyone is capable of creating these custom post types. However, they can help website administrators improve their efficiency and business profits.

If you are still unsure how to create custom post types or if you are facing any problem while creating custom post types for your website, then our WordPress experts can help you get the job done. Our WordPress experts are known for their years of service and they can definitely help you fix your WordPress problems once and for all.

Get in touch with WordPress Solutions Experts. Book a Call Today!


Updated on: 26 February 2021 |


Nirmal Gyanwali, Director of WP Creative

Nirmal Gyanwali

With over 16 years of experience in the web industry, Nirmal has built websites for a wide variety of businesses; from mom n’ pop shops to some of Australia’s leading brands. Nirmal brings his wealth of experience in managing teams to WP Creative along with his wife, Saba.