Create a custom WordPress taxonomy for posts and pages

By Dillon Smart · · · 0 Comments

Sometimes when building a website using WordPress, the standard taxonomies Categories and Tags just aren’t enough, and you need to create more. Let’s learn how to create our own custom WordPress taxonomy.

Wordpress Post Taxonomy

WordPress has made creating your own taxonomy easy, and you can even add your custom taxonomies to pages too. To accomplish this we use the custom WordPress taxonomy function register_taxonomy().

Creating a custom WordPress taxonomy

To create a new WordPress Taxonomy, we will need to use the register_taxonomy function which comes built-in to WordPress.

WordPress has hundreds of these functions you can use to customize your WordPress website. If you are unfamiliar with making changes to the code of your WordPress theme, or unfamiliar with PHP the programming language WordPress is built in, check out this post which explains and answers the question what is PHP?

In the snippet below, we create a new custom taxonomy called Services and add it to the Pages post type.

add_action( 'init', 'create_taxonomy' );
function create_taxonomy() {
    register_taxonomy(
        'Service',  
        'page',  
        array(
            'label' => 'Service', // the name of your new taxonomy
            'hierarchical' => true,
        )
    );
}

Copy and paste the snippet into your functions.php file, which is located in the root directory of your theme.

Notice in the code the comment next to ‘label’. You can change this to whatever suits your needs best. In this case, I want to create a new WordPress Taxonomy called Services.

Now, if you navigate to the WordPress admin area, and select pages, you will notice a new label called Services.

Conclusion

You can also read up on WordPress taxonomies here.

PHPWordPress

0 Comment

Was this helpful? Leave a comment!

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

List of programming languages used for Web Development

Updated 16th August 2022

There are a wide variety of programming languages that can be used for web development. Which one you choose depends on your preferences and needs. Some languages are better suited for certain tasks than others. In this post, we’ll take a look at some of the most popular languages used for web development. JavaScript PHP

2022 PHP Usage Statistics

Updated 16th August 2022

PHP: Hypertext Preprocessor is the most popular language used on the web. Around 77.6% of all websites run PHP in some way as of April 2022. PHP has been the driving force behind the growth of the web, but is it slowly dying? Let’s look at some PHP usage statistics. Why is PHP so popular?

PHP 8.3 release and features

Updated 11th December 2023

The time of year is fast approaching for PHP developers around the world. We have a new PHP release knocking on the door. PHP 8.3 RC 1 is now available for testing, and with this release we are able to get a glimps at some of the highlight features coming later in the year. PHP