themes

How to Create a Custom Post Type in WordPress

Use the snippet below to register a Testimonials custom post type in WordPress. Add it to your theme’s functions.php file (or a small custom plugin), then save and refresh Permalinks.

PHP code

/* Register Testimonials custom post type */
function qrs_testimonial_posttype() {
	$labels = array(
		'name'               => _x( 'Testimonials', 'qrs' ),
		'singular_name'      => _x( 'Testimonial', 'qrs' ),
		'add_new'            => _x( 'Add New', 'qrs' ),
		'add_new_item'       => __( 'Add New Testimonial' ),
		'edit_item'          => __( 'Edit Testimonial' ),
		'new_item'           => __( 'New Testimonial' ),
		'all_items'          => __( 'All Testimonials' ),
		'view_item'          => __( 'View Testimonial' ),
		'search_items'       => __( 'Search Testimonial' ),
		'not_found'          => __( 'No Testimonial found' ),
		'not_found_in_trash' => __( 'No Testimonials found in the Trash' ),
		'menu_name'          => 'Testimonials',
	);

	register_post_type(
		'testimonials',
		array(
			'labels'       => $labels,
			'public'       => true,
			'has_archive'  => false,
			'supports'     => array( 'title', 'editor' ),
			'rewrite'      => array( 'slug' => 'testimonials' ),
			'show_in_rest' => true,
		)
	);
}
add_action( 'init', 'qrs_testimonial_posttype' );

Next steps

  • You should now see a Testimonials menu in the WordPress admin.
  • Optionally add thumbnail to the supports array if you need featured images.

Leave a reply

Your email address will not be published. Required fields are marked *