How to loop through 1 CPT with 2 Taxonomies

You probably want to use wp_get_post_terms().

  • If a product is always associated to only one category – which has no child if hierarchical – (what I understand from your example), adding below snippet within your while loop should do it (not tested).

    <?
    $cat = wp_get_post_terms( get_the_ID(), 'department' );
    echo '<span>(' . $cat->name . ')</span>';
    ?>  
    
  • If however a product can have several categories, it’s not much more complicated. Above $cat will have to be $cats – just add a foreach loop to go throught it. In this case you may also want to specify the 3rd parameter of wp_get_post_terms(), which is by default the array $wp_gpt_args=array('order' => 'ASC', 'orderby' => 'name', 'fields' => 'all'). You will find the various options in the Codex article on wp_get_object_terms, that is actually called by wp_get_post_terms() and share the same arguments.


EDIT 24/10/2014

I’ve modified the snippet above with the correct taxonomy name and added the non hierarchical condition to the first point.

As it seems you don’t need the full object to be returned, I added in the snippet below one argument so that the function will only return terms’ names. From your comments, I can imagine $cat will now be an array of strings containing both parents and children terms.

To only display the top parent terms, you can apply either the trick suggested here or the idea behind it, which would give the following:

<?
$wp_gpt_args = array( 'fields' => 'names' ); 
$deps = wp_get_post_terms( get_the_ID(), 'department', $wp_gpt_args );
$top_dep = $deps[0]; //i.e. the first line of $deps array
echo '<span>(' . $top_dep . ')</span>';
?>