How to display post type categories with shortcode in wordPress ?
We can use shortcode for displaying custom post type categories. Here is an example shortcode.
function display_custom_post_type_categories($atts) {
// Set default attributes for the shortcode
$atts = shortcode_atts(
array(
'post_type' => 'portfolio', // Change this to your custom post type
),
$atts,
'post_type_categories'
);
$args = array(
'taxonomy' => 'portfolio-category', // Change this as your custom taxonomy
'post_type' => $atts['post_type'],
'orderby' => 'name',
'order' => 'ASC',
);
// Get the categories
$categories = get_terms($args);
// display categories
if ($categories) {
$output = '<ul>';
foreach ($categories as $category) {
$output .= '<li><a href="' . esc_url(get_term_link($category)) . '">' . esc_html($category->name) . '</a></li>';
}
$output .= '</ul>';
} else {
$output = 'No categories found for this post type.';
}
return $output;
}
// Register the shortcode
add_shortcode('post_type_categories', 'display_custom_post_type_categories');
Now add this short code [post_type_categories] to your wordPress editor.