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