How to Create Custom Post Type in Wordpress

How to Create Custom Post Type in WordPress Without Plugin

Creating a custom post type in WordPress allows you to create and manage specific types of content beyond the default posts and pages. Here’s how to do it:

Step 1: Access Your Theme or Plugin

To create Custom Post Type in WordPress open your WordPress theme’s functions.php file (recommended for small changes) or create a custom plugin (better for portability).

Step 2: Use the register_post_type() Function

Add the following code to your functions.php file or plugin:

function create_custom_post_type() {
$labels = array(
'name' => _x( 'Books', 'Post Type General Name', 'textdomain' ),
'singular_name' => _x( 'Book', 'Post Type Singular Name', 'textdomain' ),
'menu_name' => __( 'Books', 'textdomain' ),
'name_admin_bar' => __( 'Book', 'textdomain' ),
'add_new_item' => __( 'Add New Book', 'textdomain' ),
'edit_item' => __( 'Edit Book', 'textdomain' ),
'new_item' => __( 'New Book', 'textdomain' ),
'view_item' => __( 'View Book', 'textdomain' ),
'all_items' => __( 'All Books', 'textdomain' ),
'search_items' => __( 'Search Books', 'textdomain' ),
'not_found' => __( 'No books found', 'textdomain' ),
'not_found_in_trash' => __( 'No books found in Trash', 'textdomain' ),
);

$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'books' ),
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' ),
'menu_icon' => 'dashicons-book', // Optional icon
'show_in_rest' => true, // Enable Gutenberg support
);

register_post_type( 'book', $args );
}
add_action( 'init', 'create_custom_post_type' );

Step 3: Save and Refresh

  • Save the file and refresh your WordPress dashboard.
  • A new menu item called “Books” will appear in the admin panel.

Step 4: Optional Features

  1. Add Taxonomies: Use register_taxonomy() to create custom categories or tags for your post type.
  2. Custom Templates: Create a template file like single-book.php or archive-book.php in your theme folder for custom display.
function create_book_taxonomy() {
register_taxonomy(
'genre',
'book',
array(
'label' => __( 'Genre', 'textdomain' ),
'rewrite' => array( 'slug' => 'genre' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'create_book_taxonomy' );

 

By following these steps, you can create and customize your post type for any specific type of content!


Need Help? Contact us:

+91 98826 06526
Web.Expert1380
+91 98826 06526
FaceBook
support@modulesgarden.in

https://modulesgarden.in