Get awesome content delivered straight to your inbox.No Spam Guranteed
In this session you are going to learn the process to create Custom Post Types and Taxonomies. First and foremost, before creating custom post types we should have the proper knowledge of what and why is CPT used in WordPress. Custom Post Types is a key feature that has helped WordPress transform into a proper robust content management system (CMS). Let’s take a closer look at how these custom post types are created and implemented.
Simply put, custom post types is a WordPress post that contains custom defined parameters such as custom fields, category, tags, etc. One can create any type of content using different parameters. Books, portfolios, reviews, etc are some example of custom post types. Many of you readers out there have some level of WordPress knowledge may know that there are some default post types used by WordPress. If not there is no problem, as they are listed below:
There is no harm in separating post types using the regular WP posts, categories, and tags. But it might get complicated when some contents need to be separated from other post types, or maybe you want the content to display differently than other post types. There are two ways to use custom post types.
The first one is by using plugin which is a good method for users that are unfamiliar with PHP codes. It will register the custom post type for you.
The second method is by manually registering the custom post type which basically is writing the code by yourself. In this blog, we are going to skip the plugin portion as we are focusing on creating custom post types manually. But if you want it the easy way out, you can always check Custom Post Type UI. This is the most popular free plugin in WordPress and provides documentation on how you can set it up. The main problem we try to skip plugin use is that the custom post types that you created will disappear when the plugin is deactivated. The data that are stored on those custom post types will still be there, but your custom post type will be unregistered and inaccessible from the admin section.
Moving on, we are here to create custom post types with code. The code simply can be added in your theme’s functions.php file. Below is a working example so that you understand how it works.
/* Custom Post Type Start */
function create_posttype() {
register_post_type( 'books',
// CPT Options
array(
'labels' => array(
'name' => __( 'Books' ),
'singular_name' => __( 'Books' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'books'),
'menu_icon' => 'dashicons-book',
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
/* Custom Post Type End */
This code registers a post type ‘books’ with an array of arguments. This array has two parts. The first one is labeled, which is an array itself. The second part consists of other arguments like public visibility, has archive and slug which will be used in URLs for this post type.
All you need to do is simply go to Appearance > Menus and add a custom link to your menu. This will be the link to your custom post type. If you are using SEO friendly permalinks, then the URL of your custom post type will be something like this:
http://example.com/books
If you are not using SEO friendly permalinks, then your URL of custom post type will be something like this:
http://example.com/?post_type=books
As you all know, the example.com will be replaced by your domain name and books will be your custom post type name. Save your menu and visit the front-end of your site. The new menu should be added, and after clicking it displays your custom post type archive page using the archive.php template file in your theme.
You can dedicate template for custom post type archives if you don’t like the appearance of the archive page for your custom post type. For this, all you need to do is create a new file in your theme directory and name it archive-books.php. Replace books with the name of your custom post type.
To start it in a simple way, you can copy the content of your theme’s archive.php file into archive-books.php template and then custom accordingly. This archive-books.php template will be used to display whenever the archive page for your custom post type is accessed.
The same method goes with single.php templates as well. We can copy the contents of single.php into single-books.php template and then start customizing it to meet your needs.
In this section, we will be talking about creating custom taxonomies in WordPress as well as how to display them in your WordPress theme. But first let’s grasp the meaning and use of taxonomies, shall we?
Taxonomy in WordPress is used by everyone even though they have no clue using it. These WordPress taxonomies are used as a way to group posts and custom post types together. WordPress contains two popular inbuilt taxonomies that people often use: Categories and Tags. Custom Taxonomies can be used to create custom groups and bring them under a single one umbrella. Let’s assume, you have a custom post type called Books. Categories can be used in this particular matter but you not want to mix the two as they are used differently. You can register a new custom taxonomy let’s say Genre. You can also add genres like Romance, Fantasy, Non-Fiction, etc. This would allow you to sort books according to their Genre. You can also have sub-topics such as fiction would have thrillers as a sub-topic making taxonomies hierarchical.
After knowing what is a custom taxonomy, now let’s move on to creating custom taxonomies in WordPress. Just like creating custom post types, we use two methods for creating taxonomies. The first method would be utilizing a plugin without dealing with code. And the second method will be manually created using code.
The first method to create custom taxonomy is by installing and activating a WordPress plugin such as Simple Taxonomy. After installing and activating the plugin. Go to Settings > Custom Taxonomies and create a new taxonomy. Or, you can create your custom taxonomy simply by following the steps included in the description of the plugin.
Let’s move on to creating taxonomies manually. First of all, add the following code in your theme’s functions.php file to create a hierarchical custom taxonomy like categories:
// Register Custom Taxonomy
function custom_genre() {
$labels = array(
'name' => _x( 'Genre', 'Genre General Name', 'text_domain' ),
'singular_name' => _x( 'Genre', 'Genre Singular Name', 'text_domain' ),
'menu_name' => __( 'Genre', 'text_domain' ),
'all_items' => __( 'All Genre', 'text_domain' ),
'parent_item' => __( 'Parent Item', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'new_item_name' => __( 'New Genre', 'text_domain' ),
'add_new_item' => __( 'Add New Genre', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular Items', 'text_domain' ),
'search_items' => __( 'Search Items', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No Genre', 'text_domain' ),
'items_list' => __( 'Genre list', 'text_domain' ),
'items_list_navigation' => __( 'Genre list navigation', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'genre', array( 'books' ), $args );
}
add_action( 'init', 'custom_genre', 0 );
This is the way to display the terms you have added to a custom taxonomy on your single post page. The code below is the way I use that is added in single.php file within the loop:
$categories = get_the_terms( $post->ID, 'genre' );
foreach ($categories as $key => $cat) {
$name = $cat->name;
$link = $cat->term_id;
?>
<h1>Genre: <a href="<?php echo get_category_link( $link );?>"><?php echo $name; ?></a></h1>
<?php
}
?>
You can also add this code in other files such as archive.php, index.php, and anywhere else you see fit to display the taxonomy.
Custom taxonomies use the archive.php template to display posts by default. But, you can always create a custom archive display for them by creating taxonomy-{taxonomy-slug}.php. I have created taxonomy-books.php to display posts.
Some other articles related to WordPress Basics:-