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,).

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.