0

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"
 }
}


1 Answer 1

1

Instead of echoing right away, save the category names into an array, and then you can implode the array, which means output it with whatever separators between items you wish:

<?php
if(get_the_terms($post->ID, 'kernal_category', true)) {
        // Create an empty array
        $categories = [];
        $kernal_category = get_the_terms($post->ID, 'kernal_category', true);
        // Save each category to the array
        foreach ($kernal_category as $category) {
              $categories[] = $category->name;
        }
        // Now output with implode
        echo implode(' > ', $categories);
}
?>

That way the spaces and caret separator will go between the items, but not after the last item.

5
  • Thanks for the reply - I see what you're getting out but I'm getting an Warning: implode(): Invalid arguments passed error from the implode function. If I print_r the $categories array I get: ArrayReference & Tools
    – James
    Jan 27, 2020 at 18:22
  • The problems seems to be that during the foreach loop the $categories array is somehow getting formatted as a string. When I do var_dump it is a string being output, not the expected Array.
    – James
    Jan 27, 2020 at 18:35
  • 1
    Edited above - it should be $categories[] = to add to the array instead of $categories .= which concatenates a string. Hadn't had enough caffeine. :)
    – WebElaine
    Jan 27, 2020 at 20:07
  • Brilliant, that worked. Thanks!
    – James
    Jan 27, 2020 at 20:47
  • A slight expansion on above. If my taxonomy has more than 2 levels it s not? ordering them Primary, Secondary, Tertiary... is there a simple way of ordering this
    – James
    Jan 29, 2020 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.