How to Create Custom Post Types and Taxonomies in WordPress
WordPress is a highly flexible content management platform (CMS) that offers the ability to extend the functionality of your website through custom post types and taxonomies. This allows you to create content structures specific to your needs and organize them in an easy and efficient way. In this post, we'll look at how to create custom post types and taxonomies in WordPress and how to use them in your projects.
1. Custom Post Types
Custom Post Types (CPT) are specific types of content that you can create and manage within your WordPress site. They allow you to create different types of posts that are different from standard articles and pages, such as portfolio, products, reviews, and more.
1.1. Create a custom post type
Creating a CPT is a fairly simple process and can be done by adding code to your WordPress theme's functions.php file or by using a plugin such as Custom Post Type UI.
Here is an example of the code you can add to your theme's functions.php file to create a CPT named "Products":
function create_custom_post_type() { register_post_type('products', array( 'labels' => array( 'name' => __('Products'), 'singular_name' => __('Product') ), 'public' => true, 'has_archive' => true, 'supports' => array('title', 'editor', 'thumbnail'), 'menu_icon' => 'dashicons-cart', ) ); } add_action('init', 'create_custom_post_type');
After adding the code and saving the changes, you will see the new "Products" post type in the WordPress admin area.
1.2. Using templates for custom post types
To create templates that apply only to a specific CPT, you must follow the standard file naming convention. For example, if you have a CPT named "Products," you might create a file named "single-products.php" for the individual posts and "archive-products.php" for the archive pages.
2. Custom taxonomies
Custom taxonomies are specific types of categories or labels that can be associated with your CPTs. They allow you to organize and group your content more efficiently.
2.1. Create custom taxonomies
Creating custom taxonomies can be done by adding code to your theme's functions.php file or by using a plugin like Custom Post Type UI.
Here's some sample code you can add to your theme's functions.php file to create a "Product Categories" taxonomy to link to the "Products" CPT:
function create_custom_taxonomy() { register_taxonomy('product_categories', array('products'), array( 'labels' => array( 'name' => __('Product Categories'), 'singular_name' => __('Product Category ') ), 'public' => true, 'hierarchical' => true, 'rewrite' => array('slug' => 'product-categories') ) ); } add_action('init', 'create_custom_taxonomy');
Once you've added the code and saved the changes, you'll see the new Product Categories taxonomy in the WordPress admin associated with your Products CPTs.
2.2. Using templates for custom taxonomies
To create templates that only apply to a specific taxonomy, you must follow the standard file naming convention. For example, if you have a "Product Categories" taxonomy, you can create a file named "taxonomy-product_categories.php" to display the posts from a specific product category.
Creating custom post types and taxonomies in WordPress allows you to expand the structure of your content and organize it more efficiently. It's an excellent way to create unique and customized websites that meet the needs of your users and your business strategy. Follow the steps above to create your own CPTs and taxonomies and use them in your WordPress projects.