Visualizzare il nome della categoria di un tipo di post personalizzato
Ho una query personalizzata, in cui sto visualizzando alcuni post risultanti da un tipo di post personalizzato chiamato "staff". Questo custom post type è collegato a una tassonomia personalizzata chiamata "department". Riesco a visualizzare i risultati, ma non riesco a stampare la categoria collegata a ciascun post.
Questo è il mio codice:
<?php
$args = array(
'post_type' => 'staff', 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'departments',
'field' => 'slug',
'terms' => 'board'
)
)
);
$loop = new WP_Query( $args );
?>
<?php if( $loop->have_posts() ): ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<p class="text-center name"><?php the_title(); ?></p>
<?php the_category(' '); ?>
<?php endwhile; ?>
<?php endif; ?>
Penso che il problema sia che sto usando the_category ma non sono sicuro.
Qualche idea su cosa potrebbe essere sbagliato?
Grazie!

utilizza termini come questo:
$terms = get_the_terms($post->ID, 'Inserisci_la_tua_tassonomia_qui' );
if ($terms && ! is_wp_error($terms)) :
$tslugs_arr = array();
foreach ($terms as $term) {
$tslugs_arr[] = $term->slug;
}
$terms_slug_str = join( " ", $tslugs_arr);
endif;
echo $terms_slug_str;

Nel caso qualcuno stia cercando questa soluzione nel 2019. Con questo codice otterrai il nome del CUSTOM POST TYPE con l'URL
$terms = wp_get_post_terms( $post->ID, 'INSERISCI-QUI-LA-TUA-TASSONOMIA');
foreach ( $terms as $term ) {
$term_link = get_term_link( $term );
echo '<a href="' . $term_link . '">' . $term->name . '</a>' . ' ';
}

$categories = get_terms(array( 'taxonomy' => 'course_categories', 'hide_empty' => false, ));
$output .= '<select name="category">';
$output .= '<option value="">Seleziona Categoria</option>';
foreach ($categories as $category) {
$output .= '<option value="' . $category->slug . '">' . $category->name . '</option>';
}
if (!empty($category_data)) { // Se è selezionata una categoria, filtra per quella categoria $post['tax_query'] = array( array( 'taxonomy' => 'course_categories', 'field' => 'slug', 'terms' => $category_data, 'operator' => 'IN', ), ); }
