<\/span><\/h3>\nAfter defining your content, you can begin customizing your post type. You’ll have to initiate the register_post_type() function to create a new post type in WordPress. Here’s what you need to do:<\/p>\n
function custom_post_type() {<\/p>\n$labels = array(
\n 'name' => 'Products',
\n 'singular_name' => 'Product',
\n);<\/p>\n
$args = array(
\n 'labels' => $labels,
\n 'public' => true,
\n 'has_archive' => true,
\n 'menu_icon' => 'dashicons-cart',
\n 'supports' => array(
\n 'title',
\n 'editor',
\n 'thumbnail',
\n 'excerpt',
\n 'custom-fields',
\n ),
\n);<\/p>\n
register_post_type( 'product', $args );
\n}<\/p>\n
add_action( 'init', 'custom_post_type' ); <\/code><\/p>\n
In the above example, we are creating a new post type known as “Product.” We are using an array to define various labels for the post type, and we have set the post type to be public for visitors to access the site easily. <\/p>\n
<\/span>Step 3: Define Your Custom Taxonomies<\/span><\/h3>\nIf you want to create custom taxonomies for your post type, the register_taxonomy() function can be useful. For example:<\/p>\n
function custom_taxonomy() {<\/p>\n$labels = array(
\n 'name' => 'Product Categories',
\n 'singular_name' => 'Product Category',
\n);<\/p>\n
$args = array(
\n 'labels' => $labels,
\n 'public' => true,
\n 'hierarchical' => true,
\n);<\/p>\n
register_taxonomy( 'product_category', 'product', $args );
\n}<\/p>\n
add_action( 'init', 'custom_taxonomy' ); <\/code> <\/p>\n
In this instance, we created a taxonomy called “Product Categories” and defined labels using an array. We then set it to being hierarchical, making it possible to create sub-categories within other categories.<\/p>\n
<\/span>Step 4: Customize Your Template Files<\/span><\/h3>\nAfter creating your custom post type and taxonomies, you can leverage them to create custom templates for exhibiting your content. WordPress will automatically look for template files for your custom post type.<\/p>\n