Displaying the category name of a custom post type

So I have a custom query, in which I’m displaying some results posts of a custom post type called “staff”. This custom post type is linked to a custom taxonomy called “department”. I am able to display results, but I am unable to print the category that’s linked to each post.

This is my code:

        <?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; ?>

I think the problem is that I’m using but I’m not sure.

Any ideas what could be wrong?

Thanks!

3 s
3

So this is what I needed:

<?php
$terms = get_the_terms( $post->ID , 'board' );
foreach ( $terms as $term ) {
echo $term->name;
}
?>

Leave a Comment