Support

Account

Home Forums Front-end Issues Showing ACF taxonomies as classes

Helping

Showing ACF taxonomies as classes

  • Hello,
    I’m using a taxonomy field and I’m trying to show the terms used as a class name in a div.

    Something like:
    <div class="term1 term2 term3">Item</div>
    Where these terms are grabbed from the taxonomy field in the panel.

    Does anyone know how can I show the items without whitespace, commas, or anything? Just one next to the other with an space, like normal CSS classes.

    Thanks in advance

  • 
    <?php 
    $classes = array();
    $terms = get_field('tax_field_name');
    if ($terms) {
      foreach ($terms as $term) {
        $classes[] = $term->name;
      }
    }
    ?>
    <div class="<?php echo implode(' ', $classes); ?>">
    
    </div>
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.