I have read similar questions but I'm not getting a complete understanding of how to make this work.
I have created a custom post type and for it a custom taxonomy called kernal_category.
I am displaying my custom posts on the archive page and can output the lowest tier category using the code below:
if(get_the_terms($post->ID, 'kernal_category', true)) {
$kernal_category = get_the_terms($post->ID, 'kernal_category', true);
foreach ($kernal_category as $category) {
echo $category->name.' > ';
# code...
}
}
This is fine for posts that only have one term. But I have multiple posts which have tiered taxonomy. I want my code to display these taxonomies in full like a breadcrumb. So if there is only one term it should be as below:
News
If there are multiple it should be as below:
News > Studio News > America
To achieve this I want to add > symbols between all but the last term. How can I do this since with the foreach loop I don't know how to instruct my code to not add the '>' symbol after the last term.
What I get currently is: News > Studio News > America >
I've tried to understand how to use While loop with objects but can't understand it. An example of my Array Object with multiple terms below:
object(WP_Term)#3621 (10) {
["term_id"]=>
int(7)
["name"]=>
string(4) "News"
["slug"]=>
string(4) "news"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(7)
["taxonomy"]=>
string(15) "kernal_category"
["description"]=>
string(0) ""
["parent"]=>
int(0)
["count"]=>
int(1)
["filter"]=>
string(3) "raw"
}
[1]=>
object(WP_Term)#3625 (10) {
["term_id"]=>
int(9)
["name"]=>
string(11) "Studio News"
["slug"]=>
string(11) "studio-news"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(9)
["taxonomy"]=>
string(15) "kernal_category"
["description"]=>
string(0) ""
["parent"]=>
int(7)
["count"]=>
int(1)
["filter"]=>
string(3) "raw"
}
}