Wordpress

WordPress Custom Post Type

Do you want to learn how to easily create custom post types in WordPress?

In this article, we’ll show you how to clearly create WordPress Custom Post Type in WordPress in a simple and clear way. We’ll teach you two methods and you can choose one that looks easier to you.

Basically, they allow you to go beyond posts and pages by creating different content types for your website. In other words, Post types are methods of describing different types of content in WordPress. I’m hoping you’re already close with the two most common post types: posts and pages. But what you might not know is that WordPress also comes bundled with a few other post types.

Default Post Types That Come’s with WordPress are:

  1. Posts
  2. Pages
  3. Attachments
  4. Revisions

1.Posts
The “post” is a post type you’ll utilize the most in WordPress. Posts are Dynamic & Powerfull content: composed to be updated frequently. You can add categories and tags to them, meaning that they’ll be presented in multiple archive pages.

2.Pages
The post type “page” Pages can’t hold categories or tags and aren’t designed to be displayed on archive pages. Alternatively, visitors will access them via your navigation menu or custom links.

3.Attachments
The post type “attachment”, All attachment also has its own attachment page with a unique URL. You can find the URL for this via the media section in the WordPress admin panel.

4.Revisions
The post type “revision”, Each time you Update a new version of an existing post, page, attachment, or custom post type, WordPress will create a revision to reflect that version of the post or page.

You can also utilize the plugin to register a custom taxonomy and to assign that to your new post type or to any existing post type.

How to Add a Custom Post Type with a Plugin :

Method 1:

Custom Post Type UI provides an easy way to use interface for registering and managing custom post types and taxonomies for your website.

Method 2:

This method is an optimized way for WordPress to create Custom Post types because installing a plugin for every need of the customer is quite difficult and we need to take care of site performance, speed.

Now, we are going to create a custom post type called “Org” for that you need to paste the below code in active theme’s functions.php file


// custom taxonomy
function create_posttype() {
register_post_type( 'org',
// CPT Options
array(
  'labels' => array(
   'name' => __( 'Org' ),
   'singular_name' => __( 'Org' )
  ),
  'public' => true,
  'has_archive' => false,
  'rewrite' => array('slug' => 'Org'),
 )
);

}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

/*Custom Post type start*/
function cw_post_type_Org() {
$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('Org', 'plural'),
'singular_name' => _x('Org', 'singular'),
'menu_name' => _x('Org', 'admin menu'),
'name_admin_bar' => _x('Org', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New Org'),
'new_item' => __('New Org'),
'edit_item' => __('Edit Org'),
'view_item' => __('View Org'),
'all_items' => __('All Org'),
'search_items' => __('Search Org'),
'not_found' => __('No Org found.'),
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'Org'),
'has_archive' => true,
'hierarchical' => true,
);
register_post_type('Org', $args);
}
add_action('init', 'cw_post_type_Org');

The above code register’s a taxonomy called “Org” in WordPress. Using this you can create Single posts as a custom post type. As you can see from the below image :

After pasting the above code a menu has been created in the admin panel of the WordPress.

If you click on All Org, you will find the layout like this :

Now, the main thing comes up here, I,e A front end page for this custom post type. Go to your active theme’s directory search for single.php open the file using any file editor and copy all the contents from the file and paste it to a new file called single-org.php . Now you can try to open the file by clicking the view button, and if you are getting 404 not found Please go to settings->permalinks – click on save changes Button, then your work is ready…!

Happy Coding 🙂

If you find any Errors? Write it in the comments section below!

About the author

Chetan Hegde

Chetan Hegde

Chetan Hegde is the architect of ProCodersOnline.com. I'm a Software Engineer and technology enthusiast person who loves to learn and explore new things and technologies. When i'm not writing content for my websites, I love to travel and explore the nature.

Add Comment

Click here to post a comment