List of terms for custom category

Web & Digital design and development

Specializing in custom WordPress site development and graphic design

WordPress has category list block for standard categories. But what about when you want to list custom categories apart from the core ones? In that case, you would create a shortcode. Put the following code inside you child theme’s functions.php file:

function custom_taxonomy_shortcode($atts) {

    $atts = shortcode_atts(array(
        'taxonomy' => 'your_taxonomy', // Replace with the desired custom taxonomy slug
    ), $atts);

    // Retrieve the terms from the custom taxonomy
    $terms = get_terms(array(
        'taxonomy' => $atts['taxonomy'],
        'hide_empty' => false,
    ));

    if (!empty($terms)) {
        // Create a custom list of terms with links
        $term_list = '<ul>';
        foreach ($terms as $term) {
           $term_link = get_term_link($term);
            $term_list .= '<li><a href="' . esc_url($term_link) . '">' . $term->name . '</a></li>';
        }
        $term_list .= '</ul>';

        return $term_list;
    } else {
        return 'No categories found.';
    }
}
add_shortcode('custom_taxonomy', 'custom_taxonomy_shortcode'); 

Replace your_taxonomy with your taxonomy name. Then, use:

[custom_taxonomy]

You can play around with the $term variable to hide the empty categories, for example ('hide_empty' => true,).

Extra Tip

The above code will display all parent and children terms from a custom post type, all in one list. To display all parent terms with their children all grouped together, you can use the following code:

function custom_taxonomy_shortcode() {
    $taxonomy = 'dog-breed-group'; // Replace with the desired custom taxonomy slug

    // Retrieve the top-level terms from the custom taxonomy
    $parent_terms = get_terms(array(
        'taxonomy' => $taxonomy,
        'hide_empty' => false,
        'parent' => 0 // Get only top-level terms
    ));

    if (!empty($parent_terms) && !is_wp_error($parent_terms)) {
        $term_list = '<ul>';
        foreach ($parent_terms as $parent_term) {
            // Retrieve the child terms for the current top-level term
            $child_terms = get_terms(array(
                'taxonomy' => $taxonomy,
                'hide_empty' => false,
                'parent' => $parent_term->term_id // Get child terms of the current parent term
            ));

            if (!empty($child_terms) && !is_wp_error($child_terms)) {
                $term_list .= '<li>' . $parent_term->name . '<ul>';
                foreach ($child_terms as $child_term) {
                    $term_link = get_term_link($child_term);
                    $term_list .= '<li><a href="' . esc_url($term_link) . '">' . $child_term->name . '</a></li>';
                }
                $term_list .= '</ul></li>';
            }
        }
        $term_list .= '</ul>';

        return $term_list;
    } else {
        return 'No categories found.';
    }
}
add_shortcode('custom_taxonomy', 'custom_taxonomy_shortcode');

You would the use the following shortcode as before.

[custom_taxonomy]

Leave a comment

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

Ready to get started?

Contact me today to learn how I can help grow your business.