Support

Account

Home Forums ACF PRO Sub_field Taxonomy term, returning ID only… need value

Solving

Sub_field Taxonomy term, returning ID only… need value

  • Hi there,

    I’m trying to echo the value (name) of a Taxonomy term, but the generated array only gives the two sub fields and echoing of the first sub_field only gives the ID of the associated Taxonomy term.

    Backend code is below, and apologies if I’m missing something obvious, but switching the return_format doesn’t seem to have worked.

    In essence, I want to be able to output the name of the wgd_jurisdication Taxonomy, not the ID, which is currently given with:

    <?php echo $answer['ques_jurisdiction']; ?>

    ($answer is contained within a foreach loop of:

    $answers = get_field('question_answers');
    )

    <?php echo $answer['ques_answer']; ?>

    Gives the correct output (paragraphs of text).

    'sub_fields' => array (
    				array (
    					'key' => 'field_56708e6529e8e',
    					'label' => 'Jurisdiction',
    					'name' => 'ques_jurisdiction',
    					'type' => 'taxonomy',
    					'instructions' => '',
    					'required' => 0,
    					'conditional_logic' => 0,
    					'wrapper' => array (
    						'width' => '',
    						'class' => '',
    						'id' => '',
    					),
    					'taxonomy' => 'wgd_jurisdiction',
    					'field_type' => 'select',
    					'allow_null' => 0,
    					'add_term' => 1,
    					'load_save_terms' => 0,
    					'return_format' => 'id',
    					'multiple' => 0,
    				),
    				array (
    					'key' => 'field_56708c60ceb5b',
    					'label' => 'Answer',
    					'name' => 'ques_answer',
    					'type' => 'textarea',
    					'instructions' => '',
    					'required' => 0,
    					'conditional_logic' => 0,
    					'wrapper' => array (
    						'width' => '',
    						'class' => '',
    						'id' => '',
    					),
    					'default_value' => '',
    					'placeholder' => '',
    					'maxlength' => '',
    					'rows' => '',
    					'new_lines' => 'wpautop',
    					'readonly' => 0,
    					'disabled' => 0,
    				),

    Any guidance gratefully received.

  • Just to follow-up after a bit of extra digging, I need to keep the term as returning ID, as opposed to object to avoid conflicts elsewhere.

    So in essence, I’m looking for a way to pull in the taxonomy value (name) and display that instead of the ID.

  • If you have only the ID then you’ll still need to get the term in order to display the term name. See https://codex.wordpress.org/Function_Reference/get_term_by

    
    $term = get_term_by('id', $term_id, 'wgd_jurisdiction');
    echo $term->name;
    
  • Hi John, many thanks for getting back to me. I’ve tried to implement the above, but must be missing something… current template code is below, but I’m still just getting the ID (not desired) + Paragraph text (desired) outputted.

    Many thanks in advance…

    <header class="entry-header">
    <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
    </header>
    
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        
            <div class="entry-content">
                  
                    <?php
    			          
                if( have_rows('question_answers') ):
                
                    while ( have_rows('question_answers') ) : the_row();
                ?>
                
                    <div class="country-answer">
                        <h2><?php $country = get_sub_field('ques_jurisdiction');
    					echo $country;
    					$term = get_term_by('id', $term_id, 'wgd_jurisdiction');
    					echo $term->name;
    					?>
                        
                        </h2>   
    
                        <?php the_sub_field('ques_answer');?>
        
                    </div>
        
                <?php
                    endwhile;
                
                else :
                
                    // no rows found
                
                endif;
    			
    			
                
                ?>
                
                <?php  ?>
        
            </div>
        
        </article>
  • Got it! Slight adaptation of John’s code above – basically, for anyone else that might need it, I needed to pull in the value for $term_id from the ID given in the sub_field, so:

    $term = get_term_by('id', $term_id, 'wgd_jurisdiction');
    					echo $term->name;

    Becomes…

    $term = get_term_by('id', get_sub_field('ques_jurisdiction'), 'wgd_jurisdiction');
    echo($term->name);

    Hopefully this helps others – thanks again John for pointing me in the right direction.

  • Sorry I didn’t reply sooner, but glad you got it figured out.

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

The topic ‘Sub_field Taxonomy term, returning ID only… need value’ is closed to new replies.