Support

Account

Home Forums Front-end Issues How to get list of terms separated by comma from a Taxonomy field?

Solved

How to get list of terms separated by comma from a Taxonomy field?

  • How can I get a list of terms separated by comma from a taxonomy field?

    I’m actually getting the taxonomy field from a taxonomy term and wish to get each terms’ slug from the taxonomy field.

    So far, I’ve got

    <?php
    $filters = get_terms( array(
       'taxonomy' => 'filters',
       'hide_empty' => false,
       'orderby' => 'term_id',
    ) );
    
    foreach($filters as $filter):
    
       $filter_taxonomy = $filter->taxonomy;
       $filter_id = $filter->term_id;
       $filter_fields = get_field('filter-fields', $filter_taxonomy . '_' . $filter_id);
    
    endforeach;
    ?>

    I only need help “extracting” the terms’ slugs from the $filter_fields array and displaying them in a simple array separated by commas.

    UPDATE: And I would also like to be able to add the terms’ slugs as classes in a div.

    Thanks!

  • foreach($filters as $filter):
    	$filter_fields = get_field('filter-fields', $filter);
    	if( $filter_fields ):
    		$filter_fields_slugs = array_column($filter_fields, 'slug');
            $data_filter_slugs = implode(', ', $filter_fields_slugs);
    	endif;
    endforeach;
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.