0

I have made a function for use in my breadcrumbs. The breadcrumb displays custom URL's for certain taxonomy categories. It works wonderfully, however, I need help if the post is filtered under multiple categories.

<?php 

function customFruitBreadcrumbs() {
  $terms = get_the_terms( $post->ID , 'fruit_category' );

      foreach ( $terms as $term ) {
          $term_link = get_term_link( $term, 'fruit_category' );

          if( is_wp_error( $term_link ) )
          continue;

            switch ($term->name) {
              case "Spring":
                  echo '<a href="/fruits/spring-fruits/">Spring Fruits</a> » ';
                  break;
              case "Summer":
                  echo '<a href="/fruits/summer-fruits/">Summer Fruits</a> » ';
                  break;
              case "Autumn":
                  echo '<a href="/fruits/autumn-fruits/">Autumn Fruits</a> » ';
                  break;
              case "Winter":
                  echo '<a href="/fruits/winter-fruits/">Winter Fruits</a> » ';
                  break;
              default:
                  echo '';
            }

      }
}

?>

This function uses a switch statement to display the proper markup in my breadcrumbs. The problem arises when the post has 2+ categories (EG summer & spring).

How can I check the count of categories to place my switch statement if categories == 1? if > 1 category, I would just like to display:

echo '<a href="/fruits/">Fruits</a> » ';
2

You can achieve this very easily, just count the array with the terms.

Also if I could suggest something more, I'd check for WP_Error a bit earlier, outside the foreach loop, instead of checking every single term, especially that you're not using the $term_link variable anywhere.

Just checking if get_the_terms() returns an array should be enough in this case if you don't wish to print out the error message.

function customFruitBreadcrumbs() {
     $terms = get_the_terms( $post->ID , 'fruit_category' );

     // do nothing if you got an error or no terms at all
     if ( ! is_array( $terms ) ) {
          return;
     }

     // here's your link for multiple terms
     if ( count($terms) > 1 ) {
          echo '<a href="/fruits/">Fruits</a> » ';
          return;
     }

     // if you got so far you know you've got exactly one term in the array at this point,
     // so you can go with the switch and get rid of the foreach loop completely

     switch ($terms[0]->name) {
          case "Spring":
              echo '<a href="/fruits/spring-fruits/">Spring Fruits</a> » ';
              break;
          case "Summer":
              echo '<a href="/fruits/summer-fruits/">Summer Fruits</a> » ';
              break;
          case "Autumn":
              echo '<a href="/fruits/autumn-fruits/">Autumn Fruits</a> » ';
              break;
          case "Winter":
              echo '<a href="/fruits/winter-fruits/">Winter Fruits</a> » ';
              break;
          default:
              echo '';
     }
}
| improve this answer | |
  • Thank you so much @Levi Dulstein ! This worked flawlessly. Your comments helped A LOT! – Joe Aug 22 '18 at 16:23

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.