- This topic has 0 replies, 1 voice, and was last updated 5 months, 3 weeks ago by .
Viewing 0 reply threads
Viewing 0 reply threads
- You must be logged in to reply to this topic.
Welcome › mkmcst community Forum › Web programming › WordPress › Random wordpress › Create a Custom Post Type
Custom post types help you organize different content types, such as testimonials or products, and enhance user experience. You might want to consider a plugin if you’re building a complex site with multiple post types. However, if your needs are simpler, such as adding just one or two post types, a code snippet can efficiently do the job.
This code snippet lets you create a new custom post type called ‘Product’, which you can rename as needed. To use it, create a new PHP file in your theme’s main folder, like ‘product.php’, and insert the code snippet there. Then, using include() or require(), include this file in your theme’s ‘functions.php’ file.
Or, you can directly paste the code into ‘functions.php’. The code works by using the ‘init’ action hook to register the post type after WordPress loads but before the page starts loading.
function codewp_create_post_type() {
register_post_type( 'Product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
)
);
}
add_action( 'init', 'codewp_create_post_type' );