This content originally appeared on DEV Community and was authored by Almaz Bisenbaev
There are two ways to create a Custom Post Type in WordPress (that I know of). The first one involves copy&pasting a little snippet of PHP code into your functions.php, and the second one allows you to do achieve the same with a few mouse clicks.
Let’s start from the first one.
Method 1: Creating a Custom Post Type Through functions.php
For developers who prefer a hands-on approach, adding custom post types through code is a powerful method. You can define custom post types by adding code to your theme’s functions.php
file. Here’s a simple example:
function create_custom_post_type() {
$labels = array(
'name' => __('Books'),
'singular_name' => __('Book'),
'menu_name' => __('Books'),
'name_admin_bar' => __('Book'),
'add_new' => __('Add New', 'book'),
'add_new_item' => __('Add New Book'),
'new_item' => __('New Book'),
'edit_item' => __('Edit Book'),
'view_item' => __('View Book'),
'all_items' => __('All Books')
);
$args = array(
'labels' => $labels,
'public' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'supports' => array('title', 'editor', 'thumbnail')
);
register_post_type('book', $args);
}
add_action('init', 'create_custom_post_type');
Throw this piece of code into your functions.php and your custom post type called “Books” will show up in the admin menu.
This method gives you complete control over the post type’s configuration, but it requires familiarity with PHP and WordPress functions.
Method 2: Creating a Custom Post Type Using WP Power Tools
In order to avoid copy&pasting the same code through all my clients’ websites, I created this little plugin that allows me to do these repetitive tasks with a mouse mouse clicks, and I published the plugin on Github for everyone to use.
The source code of the plugin is here: https://github.com/almazbisenbaev/wp-powertools
When you install it, among other tools you will find a tool called CPT Manager. The tools does exactly the same as what you could achieve with the code above, just easier and faster.
If you tried out my plugin, don’t forget to give it a star on github, I would really appreciate it.
This content originally appeared on DEV Community and was authored by Almaz Bisenbaev
Almaz Bisenbaev | Sciencx (2024-08-21T23:27:11+00:00) The easiest and fastest way to create a Custom Post Type in WordPress. Retrieved from https://www.scien.cx/2024/08/21/the-easiest-and-fastest-way-to-create-a-custom-post-type-in-wordpress/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.