How to create custom post types in WordPress
Are you tired of using the same old post types in WordPress? Do you want to create custom post types that are tailored to your specific needs? Look no further! In this post, we'll guide you through the process of creating custom post types in WordPress. With our step-by-step instructions, you'll be able to create unique post types that will take your website to the next level. Let's get started!
When WordPress was first introduced, it is used as blogging. But nowadays, it is used for building many complicating websites. It is the most widely used CMS nowadays. There are lots of options provided in the WordPress which can use to create any type of static as well as dynamic websites. WordPress comes with post and pages to contain the content. You can also create any number of posts and named them whatever you want. These post types are known as custom post types. In this article, we will show that how we can create custom post types in WordPress.
What is Custom Post type in WordPress?
Basically, custom post types are like normal pages and posts which are used to contain content. Custom post types are used to manage your content in a more structured way. You can attach categories, tags, and authors to your custom post. By default, WordPress comes with post types like pages, post, attachment, revision, Nav menu. Here we will show how o create a custom post type in WordPress.
We can create a custom post by using two methods:
1. Using Plugin
2. Using code
1. Using Plugin
If you are a beginner, then this is the best way to create a custom post. Also, the plugin comes with a number of options which you can easily customize.
First, you need to install a plugin named Custom Post Type UI. To know How to install and activate plugin in WordPress read our previous article.
Now go to CPT UI >> Add/Edit Post Types to create a custom post type.
There are a number of options provided which you can use. Fill the details as per your need and click on Add Post Type button.
You can also add custom taxonomies using this plugin. To add new taxonomies click on Add/Edit Taxonomies. Fill the details as per your need and click on Add Taxonomy button.
Also, you can customize many options as per your need. In this fashion, you can simply add custom post types easily.
2. Using code
Using plugin is not much effective because if you deactivate or delete the plugin, your all information lost. The best way to create a custom post is to add some code to your function.php file. This is the best way because all of your information is saved till you not change your theme. But for that, you should have knowledge of coding. You can add custom post type by adding few codes in function.php file.
Paste following code into your functions.php file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
- Creating a function to create our CPT
*/
function custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Movies', 'Post Type General Name', 'twentythirteen' ),
'singular_name' => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ),
'menu_name' => ( 'Movies', 'twentythirteen' ),
'parent_item_colon' => ( 'Parent Movie', 'twentythirteen' ),
'all_items' => ( 'All Movies', 'twentythirteen' ),
'view_item' => ( 'View Movie', 'twentythirteen' ),
'add_new_item' => ( 'Add New Movie', 'twentythirteen' ),
'add_new' => ( 'Add New', 'twentythirteen' ),
'edit_item' => ( 'Edit Movie', 'twentythirteen' ),
'update_item' => __( 'Update Movie', 'twentythirteen' ),
'search_items' => ( 'Search Movie', 'twentythirteen' ),
'not_found' => __( 'Not Found', 'twentythirteen' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentythirteen' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'movies', 'twentythirteen' ),
'description' => __( 'Movie news and reviews', 'twentythirteen' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'latest' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
// Registering your Custom Post Type
register_post_type( 'movies', $args );
}
/* Hook into the 'init' action so that the function
- Containing our post type registration is not
- unnecessarily executed.
*/This code register a post type with a name ‘movies’. Also, we added other options like public visibility, has an archive, and slug that will be used in URLs for this post type, revisions and featured image.
We also added a taxonomy named ‘latest’.
Visit your dashboard and you can see Movies option is added to left side navigation.
Click on Add New to add a new post. This post is attached to Movies post type only.
In this way, you can add your own Custom Post Types in WordPress. We hope you like this article. If you have any query, you can ask in below comment box.
Conclusion
To summarize, creating custom post types in WordPress can be done by using the register_post_type() function in your theme's functions.php file or by using a plugin like Custom Post Type UI or Toolset Types. Custom post types allow you to create new types of content in WordPress that are different from the default post and page types, which can help you organize and display your content in a more structured and meaningful way. If you prefer not to code, you can use a plugin to create custom post types.
How to create custom post types in WordPress Common Asked Questions
I can help with that! However, I need to clarify that the questions and answers related to creating custom post types in WordPress may vary depending on the specific needs and requirements of each user. Nonetheless, here are some common questions and their answers:
- What are custom post types in WordPress? Answer: Custom post types are a way to create new types of content in WordPress that are different from the default post and page types. They allow you to organize and display your content in a more structured and meaningful way.
- How do I create a custom post type in WordPress? Answer: You can create a custom post type in WordPress by using the register_post_type() function in your theme's functions.php file or by using a plugin like Custom Post Type UI.
- What are the benefits of creating custom post types in WordPress? Answer: Creating custom post types in WordPress allows you to organize your content in a more meaningful way, which can improve the user experience for your visitors. It also allows you to create more targeted and specific content that is tailored to your audience's needs.
- Can I create custom post types without coding? Answer: Yes, you can create custom post types in WordPress without coding by using a plugin like Custom Post Type UI or Toolset Types.
You can also check how we can add PHP code to the post.