In my WordPress categories section, I've a Food
main category, with other sub-categories ("hamburger, pasta, main, etc"). In post page i want to show only the child categories that are filled in the post.
I want to show only child category selected. If the post is in Food > hamburger
and Food > main
, i want to show only these 2 categories on post page, instead of all categories. I tried with this code, but it will show all categories:
$categories = get_categories( array(
'taxonomy' => 'category',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
'include' => 'all',
'exclude' => '',
'exclude_tree' => 'all',
'number' => false,
'fields' => 'all',
'name' => '',
'slug' => '',
'hierarchical' => true,
'search' => '',
'name__like' => '',
'description__like' => '',
'pad_counts' => false,
'get' => '',
'child_of' => false,
'childless' => false,
'cache_domain' => 'core',
'update_term_meta_cache' => true,
'meta_query' => '',
'meta_key' => array(),
'meta_value'=> ''
));
foreach ( $categories as $category ) {
printf( '<a href="%1$s">%2$s</a><br />',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
}