Home Forums General Issues Get value from taxonomy field in wp-loop


Get value from taxonomy field in wp-loop

  • Hi! I created a loop through a custom post type. This cpt contains a taxonomy field with a single select-value. For every post in the loop i need to get the taxonomy name or id. But acutally i get noting:

                    Query the post 
                    $args = array( 'post_type' => 'kuenstler', 'posts_per_page' => -1 );
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post(); 
                        $term = get_field('kategorie');     
                        if( $term ): ?>
                            <h2><?php echo $term->name; ?></h2>
                        <?php endif; ?>
                    <?php endwhile; ?>
  • A taxonomy field will return an array of terms even if it can only have one term.

    What is being output by your code?

    You can try this to see what is being returned

    $term = get_field('kategorie'); 
    echo '<pre>'; print_r($term); echo '</pre>';
  • I just got back the id of the taxonomy. Which is actually what i need. I always thought it would give me back an array.

    Now everything works!

    Thanks for your help!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Get value from taxonomy field in wp-loop’ is closed to new replies.