1

I have 3 posts in my custom post type ('careers'). I registered a custom taxonomy of ('career_categories').

I created 3 posts and assigned two of them with a category of "Full-Time" and one with "Part-Time".

I am using the following code in my loop:

$terms = get_terms( 'career_categories', array( 'hide_empty' => true, ) );

$html_out .= '<td class="column-'. $col++ .'">';
   if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) :
      foreach( $terms as $term) :
         $html_out .= $term->name;
      endforeach;
   endif;
$html_out .= '</td>';

On the front-end this is displaying both categories on each post. I need it to show only the category selected for the post.

Currently Job 1 - Full-TimePart-Time Job 2 - Full-TimePart-Time Job 3 - Full-TimePart-Time

Expected Job 1 - Full-Time Job 2 - Full-Time Job 3 - Part-Time

1 Answer 1

2

There are two functions:

get_terms

get_terms( string|array $args = array(), array $deprecated = '' )

Which retrieves the terms in a given taxonomy or list of taxonomies.

And...

get_the_terms

get_the_terms( int|object $post, string $taxonomy )

Which retrieves the terms of the taxonomy that are attached to the post.

You should use second one in your case, I guess...

3
  • Ugh, kicking myself right now. This is correct. Thanks!
    – Darren
    Jul 25, 2018 at 22:07
  • get_object_terms is a lower level and more useful function that can also be used
    – Tom J Nowell
    Jul 25, 2018 at 23:13
  • 1
    @Darren if this is the correct answer can you mark it as correct? That way Krzysiek Dróżdż gets the reputation points and others with your problem can see this worked
    – Tom J Nowell
    Jul 25, 2018 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.