jimi-del
jimi-del

Reputation: 133

How to get ACF field value form custom taxonomy inside foreach?

I created a custom field with 'domain_url' id to add field in my taxonomy using the following code:

acf_add_local_field_group(array(
    'key' => 'group_6294fa89c564b',
    'title' => 'Domain url Field',
    'fields' => array(
        array(
            'key' => 'field_629504c3cdd67',
            'label' => 'domain url',
            'name' => 'domain_url',
            'type' => 'text',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            
            'default_value' => '',
            'placeholder' => '',
            'prepend' => '',
            'append' => '',
            'maxlength' => '',
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'taxonomy',
                'operator' => '==',
                'value' => 'news_source',
            ),
        ),
    ),
    
));

And I want to get the value inside the categories foreach, I tried this code but it didn't give a result

  <?php
   $args = array(
               'taxonomy' => 'news_source',
              
           );

   $cats = get_categories($args);

   foreach($cats as $cat) {
?>
        Name:   <?php echo $cat->name; ?>  > 
        Domain:    <?php echo get_field('domain_url' ); ?> <br/> 
           
  
      
<?php
   }
?>

Is the problem in the code? Or does the extension not support it?

Upvotes: 1

Views: 3119

Answers (1)

DubVader
DubVader

Reputation: 1072

You are missing an argument in your get_field(). You need to add an arg for the term or term object.

  <?php
   $args = array(
               'taxonomy' => 'news_source',
              
           );

   $cats = get_categories($args);

   foreach($cats as $cat) {
?>
        Name:   <?php echo $cat->name; ?>  > 
        Domain:    <?php echo get_field('domain_url', $cat ); ?> <br/> // Put the term in here.
           
  
      
<?php
   }
?>

In this case you may need an extra query, or to use the term string plus the term ID in a string.

This ACF documentation article shows some different methods of retrieving tax term fields as well.

Upvotes: 1

Related Questions