Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I create a custom post type with its own templates in WordPress?
Asked on Mar 29, 2026
Answer
Creating a custom post type in WordPress allows you to organize and display content types beyond the default posts and pages. You can also create custom templates for these post types to control their appearance.
<!-- BEGIN COPY / PASTE -->
// Register Custom Post Type
function create_custom_post_type() {
$args = array(
'label' => __( 'Books', 'text_domain' ),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'rewrite' => array( 'slug' => 'books' ),
);
register_post_type( 'book', $args );
}
add_action( 'init', 'create_custom_post_type', 0 );
// Template Hierarchy
// Create single-book.php and archive-book.php in your theme for custom templates.
<!-- END COPY / PASTE -->Additional Comment:
- Register the custom post type using the "register_post_type" function in your theme's "functions.php" file or a custom plugin.
- Use the "supports" array to define features like title, editor, and thumbnail.
- Create "single-{post_type}.php" and "archive-{post_type}.php" files in your theme to customize the single and archive views.
- Ensure your theme supports post thumbnails if you plan to use them.
- Flush permalinks (Settings → Permalinks) after registering a new post type to avoid 404 errors.
Recommended Links:
