Support

Account

Home Forums General Issues Values from a taxonomy term as a sub-field

Helping

Values from a taxonomy term as a sub-field

  • I’ve tried everything I can come up with, but am stumped. I’m sure my code snippet here is not even close, but I’d appreciate any help I can get.

    I have a repeater field called area_notes with a taxonomy sub-field called select_area. I have it set to return a term object and when I run a printr on that field I see the arrays (reproduced below). Basically I just want to be able to check the value of the slug term, and if it matches a variable that I’ll create, have it display a line of text. But I cna’t for the life of me figure out how to get the term. I’ve tried this – is it even close?

    <?php if(get_field('area_notes', 'option' )): ?>
    	<?php while(has_sub_field('area_notes', 'option')): ?>
    		<?php $terms = get_sub_field('select_area'); ?>
    	        <?php foreach ($terms as $term){ ?>
    				<h2><?php echo $term->slug; ?></h2>
    			<?php } ?>	
    	<?php endwhile; ?>
    <?php endif; ?>

    This is the array that the field is creating:

    Array
    (
        [0] => Array
            (
                [select_area] => stdClass Object
                    (
                        [term_id] => 32
                        [name] => Aurora
                        [slug] => aurora
                        [term_group] => 0
                        [term_taxonomy_id] => 33
                        [taxonomy] => area
                        [description] => 
                        [parent] => 0
                        [count] => 3
                        [object_id] => 1556
                        [filter] => raw
                    )
    
                [area_note] => Buses not running the weeks of July 28th and August 5th
            )
    
        [1] => Array
            (
                [select_area] => stdClass Object
                    (
                        [term_id] => 31
                        [name] => Markham
                        [slug] => markham
                        [term_group] => 0
                        [term_taxonomy_id] => 32
                        [taxonomy] => area
                        [description] => 
                        [parent] => 0
                        [count] => 6
                        [object_id] => 1688
                        [filter] => raw
                    )
    
                [area_note] => This is a Brampton Note!
            )
    
    )
  • Interesting timing. I had this same problem… I got this to work by adding just a little tweak.

    Change:
    <h2><?php echo $term->slug; ?></h2>

    To:
    <?php echo $term[0]->slug; ?>

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

The topic ‘Values from a taxonomy term as a sub-field’ is closed to new replies.