2

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!

  • Custom post types don't have categories. It seems that you want to retrieve the custom terms of the "departments" taxonomy. However your query is already set to only poll the "board" department so there's no point to retrieve it. If you do want to retrieve it anyway. Look at the function get_the_terms codex.wordpress.org/Function_Reference/get_the_terms – gdaniel May 22 '15 at 3:39
6

So this is what I needed:

<?php
$terms = get_the_terms( $post->ID , 'board' );
foreach ( $terms as $term ) {
echo $term->name;
}
?>
  • THANK YOU! You led me to find what my own problem was. Much appreciated. – Debbie Kurth Jul 4 '19 at 16:55
1

use terms like this :

$terms = get_the_terms($post->ID, 'Enter_your_taxonomy_here' );
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;
0

In case if someone looking for this in 2019. With this you are getting CUSTOM POST TYPE name with URL

 <?php
    $terms = wp_get_post_terms( $post->ID, 'PLACE-HERE-YOUR-TAXONOMY');
    foreach ( $terms as $term ) {
       $term_link = get_term_link( $term );
       echo '<a href="' . $term_link . '">' . $term->name . '</a>' . ' ';
       }
  ?>

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.