WordPress is a flexible platform that allows users to customize their website according to their specific needs. One of the most effective ways to organize content is through categories. Although WordPress comes with predefined post types such as posts and pages, it also allows for the creation of types of customized publications. In this article, we will explore how to add categories to a custom post type in WordPress.
What are Custom Publication Types?
Custom post types are a feature of WordPress that allows developers and site administrators to create content that doesn't fit into the default categories. This is especially useful when you need to organize specialized content. For example, a recipe site might have a custom post type for "Recipes", while a portfolio site might have one for "Projects".
Advantages of Using Custom Publication Types
- Organization: They allow a better organization of related content.
- Flexibility: Display options and input fields can be customized.
- SEO improvement: By organizing content, you can improve SEO and user experience.
Creating a Custom Publication Type
To start adding categories to a custom publication type, we first need to create that type. This can be done through the functions.php of your theme or by using a specific plugin for this purpose.
Sample Code for Creating a Custom Publication Type
The following is a basic example of how to register a new type of custom publication called "Books".
function create_book_publication_type() {
$args = array(
'label' => 'Books',
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail'),
'hierarchical' => false,
);
register_post_type('books', $args);
}
add_action('init', 'create_post_type_books');
Adding Categories to Your Custom Publication Type
Once you have created your custom post type, the next step is to add categories. To do this, we will use the register_taxonomy to register a new taxonomy that will act as categories for our publication type.
Sample Code for Adding Categories
The following code shows how to add categories to our publication type "Books".
function add_category_to_books() {
register_taxonomy('book_category', 'books', array(
'label' => 'Book Categories',
'rewrite' => array('slug' => 'category-books'),
'hierarchical' => true,
));
}
add_action('init', 'add_category_to_books');
How to Use Categories in the Administration Panel
Once you have registered the categories for your custom post type, you will see a new section in the WordPress admin panel. This is how you can manage categories:
Accessing Categories
To access the categories you have created:
- Go to Books in the administration menu.
- Click on Book Categories to add, edit or delete categories.
Visualizing Categories in the Frontend
Once categories have been added and assigned to your posts, it is essential that you can display them on the frontend of your site. This can be accomplished by modifying your theme's template files.
Sample Code to Display Categories
To display the categories in the template of a custom post type, you can use the following code in the corresponding file, as follows single-books.php:
<?php $categorias = get_the_terms(get_the_ID(), 'book_category');
if ($categorias && !is_wp_error($categorias)) {
foreach ($categories as $category) {
echo '' . esc_html($category->name) . ' ';
}
}
?>
With this code, links to the categories of each book will be displayed, allowing users to easily navigate through the different categories available.
Final Considerations
Adding categories to a custom post type in WordPress is a simple process that improves the organization and navigability of your content. With proper use of WordPress features, you can further customize the user experience and optimize your website for SEO.
