Add a custom post type to WordPress with code
This code snippet allows you to add a custom post type to WordPress. In this snippet, we are adding a new CPT called "Custom Post Type" with the slug "custom_post_type".
PHP
function create_custom_post_type() {
register_post_type( 'custom_post_type',
array(
'labels' => array(
'name' => __( 'Custom Post Type' ),
'singular_name' => __( 'Custom Post Type' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'custom-post-type' ),
)
);
}
add_action( 'init', 'create_custom_post_type' );
Snippet Feedback
Did this snippet work for you? Do you have any questions about this snippet? Leave some feedback below.
SHARED BY