Creating custom post types in WordPress allows you to extend the default content types (posts and pages) and build unique structures for different types of content on your site. Here’s a step-by-step guide on how to create a custom post type in WordPress:
Method 1: Using Code in Your Theme’s functions.php
- Open your theme’s
functions.php
file:
- Access it through your WordPress dashboard by navigating to
Appearance > Theme Editor
and selectingfunctions.php
. - Alternatively, use a code editor and access the file directly through your theme folder.
- Add Custom Post Type Code:
// Add custom post type
function custom_post_type() {
$labels = array(
'name' => _x('Custom Posts', 'post type general name'),
'singular_name' => _x('Custom Post', 'post type singular name'),
'menu_name' => _x('Custom Posts', 'admin menu'),
'add_new' => _x('Add New', 'custom post'),
'add_new_item' => __('Add New Custom Post'),
'edit_item' => __('Edit Custom Post'),
'new_item' => __('New Custom Post'),
'view_item' => __('View Custom Post'),
'search_items' => __('Search Custom Posts'),
'not_found' => __('No custom posts found'),
'not_found_in_trash' => __('No custom posts found in Trash'),
'parent_item_colon' => '',
'all_items' => __('All Custom Posts'),
'archives' => __('Custom Post Archives'),
'menu_icon' => 'dashicons-admin-post', // Choose an icon: https://developer.wordpress.org/resource/dashicons
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'custom-post'),
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
);
$args = array(
'labels' => $labels,
'description' => 'Description for Custom Post Type',
'public' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-admin-post',
'hierarchical' => false,
'has_archive' => true,
'rewrite' => array('slug' => 'custom-post'),
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
);
register_post_type('custom_post', $args);
}
add_action('init', 'custom_post_type');
- Modify the Code:
- Change instances of “Custom Post” and “custom_post” with your desired labels and post type name.
- Modify the
'supports'
array to include the features you want for your custom post type (e.g.,'excerpt'
,'page-attributes'
, etc.). - You can change the
'menu_icon'
by selecting an icon from Dashicons.
- Save Changes:
- Save the changes to your
functions.php
file.
- Check Admin Dashboard:
- Visit your WordPress admin dashboard, and you should see your new custom post type in the menu.
Method 2: Using a Custom Post Type Plugin
- Install a Plugin:
- Install and activate a custom post type plugin like “Custom Post Type UI” or “Pods.”
- Create a Custom Post Type:
- Navigate to the plugin settings (usually under
Settings > CPT UI
or similar). - Add a new custom post type by providing the necessary details such as labels, slug, and supported features.
- Save Changes:
- Save your changes, and the plugin will handle the registration of the custom post type.
Both methods achieve the same result, but using a plugin might be more user-friendly for those who are not comfortable with editing theme files directly. Choose the method that best suits your preferences and workflow.
Show Comments