Custom post type in WordPress (With Code)
Method 1: Using Code (In functions.php
)
You can create a custom post type by adding code to your theme’s functions.php
file. Follow these steps:
1. Open functions.php
- Go to your WordPress dashboard.
- Navigate to Appearance > Theme File Editor.
- In the right sidebar, click on
functions.php
.
2. Add Custom Post Type Code
Paste the following code into your functions.php
file:
function create_custom_post_type() {
$labels = array(
'name' => _x( 'Books', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Book', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Books', 'text_domain' ),
'name_admin_bar' => __( 'Book', 'text_domain' ),
'archives' => __( 'Book Archives', 'text_domain' ),
'attributes' => __( 'Book Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Book:', 'text_domain' ),
'all_items' => __( 'All Books', 'text_domain' ),
'add_new_item' => __( 'Add New Book', 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'new_item' => __( 'New Book', 'text_domain' ),
'edit_item' => __( 'Edit Book', 'text_domain' ),
'update_item' => __( 'Update Book', 'text_domain' ),
'view_item' => __( 'View Book', 'text_domain' ),
'search_items' => __( 'Search Book', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into book', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this book', 'text_domain' ),
'items_list' => __( 'Books list', 'text_domain' ),
'items_list_navigation' => __( 'Books list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter books list', 'text_domain' ),
);
$args = array(
'label' => __( 'Book', 'text_domain' ),
'description' => __( 'Book Reviews and Articles', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments', 'revisions', 'custom-fields' ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type( 'book', $args );
}
add_action( 'init', 'create_custom_post_type', 0 );
3. Customize the Code
- Replace
'Book'
with the name of your custom post type (e.g.,Movies
,Products
). - Change
'book'
inregister_post_type( 'book', $args );
to your desired slug (e.g.,movie
,product
). - Modify labels and descriptions as needed.
4. Save Changes
- Save the
functions.php
file after adding the code. - A new custom post type will appear in your WordPress dashboard with the name you specified.
Method 2: Using a Plugin (Without Coding)
If you prefer not to use code, you can use a plugin to create custom post types. Here’s how:
1. Install the Plugin
- Go to your WordPress dashboard.
- Navigate to Plugins > Add New.
- Search for Custom Post Type UI.
- Install and activate the plugin.
2. Create a Custom Post Type
- After activating, go to CPT UI > Add/Edit Post Types in the WordPress dashboard.
- In the Post Type Slug field, enter the slug (e.g.,
book
). - In the Plural Label field, enter the name (e.g.,
Books
). - In the Singular Label field, enter the singular form (e.g.,
Book
). - Configure other settings like supports, taxonomies, and visibility as needed.
3. Save the Post Type
- Click on Add Post Type.
- Your custom post type will now appear in the WordPress dashboard.