On a single post, I display the post's categories like this:
$categories = get_the_category($id);
My category hierarchy is: cat1
parent of cat2
, parent of cat3
, etc.
I would like to display only children categories (from cat2
to catN
).
I tried to use get_category_by_slug($id,'cat1')
with no results...
On a single post, I display the post's categories like this:
$categories = get_the_category($id);
My category hierarchy is: cat1
parent of cat2
, parent of cat3
, etc.
I would like to display only children categories (from cat2
to catN
).
I tried to use get_category_by_slug($id,'cat1')
with no results...
1 Answer
Reset to default 0For this specific question, Try this
$categories = get_the_category($id);
foreach($categories as $category){
if($category->parent != 0){
// Display it here
echo '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . esc_html( $category->name ) . '</a>';
}
}