Support

Account

Home Forums General Issues Get value from custom field taxonomy

Helping

Get value from custom field taxonomy

  • can someone help me adapt the code?

    I have an ACF with the field type “taxonomy”. I would love to spend this.

    With the following code, however, all available taxonomies are displayed and not just the one in which the article is located?!?

    
    <?php $zielgruppe = get_field( 'zielgruppe' ); ?>
    <?php if ( $zielgruppe ) : ?>
        <?php $get_terms_args = array(
            'taxonomy' => 'zielgruppe',
            'hide_empty' => 0,
            'include' => $zielgruppe,
        ); ?>
        <?php $terms = get_terms( $get_terms_args ); ?>
        <?php if ( $terms ) : ?>
            <?php foreach ( $terms as $term ) : ?>
                <a href="<?php echo esc_url( get_term_link( $term ) ); ?>"><?php echo esc_html( $term->name ); ?></a>
            <?php endforeach; ?>
        <?php endif; ?>
    <?php endif; ?>
    

    https://ibb.co/PM98dsg
    The aim is to create a button afterwards where the content is listed that has this taxonomy.

  • To you have your taxonomy field set to return Term Objects or Term ID? include must be an array of Term IDs.

    You can override the field setting and get just the IDs

    
    <?php $zielgruppe = get_field( 'zielgruppe', false, false ); ?>
    

    Your field may also be returning only a single value and you need an array

    
    if ($zielgruppe) {
      if (!is_array($zielgruppe)) {
        $zielgruppe = array($zielgruppe);
      }
      ....
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.