Custom Taxonomy

Register a custom taxonomy in WordPress with hierarchical support.

Creates a hierarchical custom taxonomy for portfolio items with REST API support, admin column display, and a helper function to display terms with links.
custom-taxonomy.phpphp
<?php
/**
 * Register Custom Taxonomy - Portfolio Categories
 * Add to functions.php or custom plugin
 */
function create_portfolio_taxonomy() {
    $labels = array(
        'name' => 'Portfolio Categories',
        'singular_name' => 'Portfolio Category',
        'search_items' => 'Search Categories',
        'all_items' => 'All Categories',
        'parent_item' => 'Parent Category',
        'parent_item_colon' => 'Parent Category:',
        'edit_item' => 'Edit Category',
        'update_item' => 'Update Category',
        'add_new_item' => 'Add New Category',
        'new_item_name' => 'New Category Name',
        'menu_name' => 'Categories',
    );

    $args = array(
        'hierarchical' => true, // Like categories
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'portfolio-category'),
        'show_in_rest' => true, // Gutenberg support
        'rest_base' => 'portfolio-categories',
    );

    register_taxonomy('portfolio_category', array('portfolio'), $args);
}
add_action('init', 'create_portfolio_taxonomy');

/**
 * Display taxonomy terms
 */
function display_portfolio_categories($post_id) {
    $terms = get_the_terms($post_id, 'portfolio_category');
    
    if ($terms && !is_wp_error($terms)) {
        $term_list = array();
        foreach ($terms as $term) {
            $term_list[] = '<a href="' . get_term_link($term) . '">' . $term->name . '</a>';
        }
        echo implode(', ', $term_list);
    }
}

Usage

Add to functions.php. Display with: display_portfolio_categories(get_the_ID());

Installation

Add to theme's functions.php

Let’s Build Something You’ll Be Proud Of

No fluff. Just thoughtful design and reliable development.

Work with me
Average response time: within 24 hours