• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
logo

Tutsplanet

Free Technical and Blogging Resources

  • Home
  • Web Hosting
  • Programming
  • Plugins
  • Write For US
  • News
  • About Us
  • Snippets
You are here: Home / PHP / How To Create A Custom Taxonomy In WordPress

How To Create A Custom Taxonomy In WordPress

Aug 19, 2020 by Editorial Staff Leave a Comment

Today’s tutorial we are showing how to create a custom taxonomy in WordPress. After WordPress 3 this feature is included in the core, so we just need to use the function with some params. The core developers created register_taxonomy() function to handle the heavy lifting for us.

Why Custom Taxonomies?

As your system/website grows you feel the default categories in WordPress is not enough to classify the articles in the website.  So it would be beneficial if you create your own classification. But how do you create your own taxonomy in WordPress ?. The good news is WordPress allows developers to create Custom Taxonomies easily.

For example if you are making a gift catalogue website. You will have many products in different categories and gifts aimed for different groups of people such as gifts for him, gifts for her. If you have already used the categories taxonomy, so there should be a new one. It’s very easy to create a custom taxonomy in WordPress, just use the code.

You can use this code in functions.php in themes or in your module file.

function wporg_register_taxonomy_gifts()
{
    $labels = [
        'name'              => _x('Gifts', 'taxonomy general name'),
        'singular_name'     => _x('Gift', 'taxonomy singular name'),
        'search_items'      => __('Search Gifts'),
        'all_items'         => __('All Gifts'),
        'parent_item'       => __('Parent Gift'),
        'parent_item_colon' => __('Parent Gift:'),
        'edit_item'         => __('Edit Gift'),
        'update_item'       => __('Update Gift'),
        'add_new_item'      => __('Add New Gift'),
        'new_item_name'     => __('New Gift Name'),
        'menu_name'         => __('Gifts'),
    ];
    $args = [
        'hierarchical'      => true, // make it hierarchical (like categories)
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'show_in_quick_edit'=> true,
        'show_in_rest'      => true,
        'rewrite'           => ['slug' => 'gift'],
    ];
    register_taxonomy('gift', ['post'], $args);
}
add_action('init', 'wporg_register_taxonomy_gifts');
Important Information

If you are using the Gutenberg editor you should set the ‘show_in_rest’ => true, otherwise new custom taxonomy will not appear in the post add/edit screen.

 

 


Editorial Staff

Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.

View all posts by Editorial Staff

Filed Under: PHP, Programming Tagged With: Wordpress

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar




Quick Links

  • A Simple YouTube Video Downloader Script in PHP
  • The 50 Most Useful jQuery Plugins for Frontend Development
  • Base64 Decode Online
  • Base64 Encode Online

Subscribe

* indicates required

Share

   

Hot topics

  • A Simple YouTube Video Downloader Script in PHP 272 views
  • Replace “\n” with new line characters, using Notepad++ 246 views
  • Add FTP/SFTP in Visual Studio Code 183 views
  • Using Third-Party Libraries in Codeigniter 143 views
  • Open a URL in a new tab using JavaScript 141 views
  • Upload Multiple Images and Store in Database using PHP and MySQL. 130 views
  • Laravel Image Intervention Tutorial With Example 125 views
  • Hierarchical Tree view Category Example in Laravel 114 views
  • Spout, an awesome library for reading and writing in Excel. 103 views
  • Manually Install APK Files In Android Studio Emulator 98 views

Categories

  • Design & Development
  • Drupal
  • Facebook
  • General
  • How To
  • ios
  • Javascript
  • Linux
  • Magento
  • Marketing
  • News
  • PHP
  • Plugins
  • Programming
  • Snippets List
  • Social Media
  • Softwares
  • Themes
  • Tips
  • Wordpress
  • YouTube

Copyright © 2021 · TutsPlanet Gene Theme on Genesis Framework · Powered By BunnyCDN