Support

Account

Home Forums ACF PRO Multiple Taxonomies in a Repeater?

Solved

Multiple Taxonomies in a Repeater?

  • Hi!

    I don’t know if this is possible or advisable, or if I’m barking up the wrong tree…

    I have a custom post type where I need to have multiple names, with each name being linked to one or more titles. Names and titles are separate taxonomies:

    NAME TITLE
    Joe Blow: employee
    Jane Doe: owner
    William Idol: employee, manager
    Mike Smith: contractor, hr
    etc…

    Each post will have varying name/title combinations – most names will be used in multiple posts, and the title for each name will change for each post.

    The taxonomies are in a repeater called People.

    I have this working so I can add/delete names/titles, but I can’t get it to display on the front end. I’ve looked at the docs, but can’t figure out what to do. Any help would be appreciated.

  • To display the rows of a repeater with taxonomy fields as subfields would look something like this.

    This code assumes that you are returning term objects from the taxonomy fields

    
    if (have_rows('repeater_field')) {
      while (have_rows('repeter_field')) {
        the_row();
        $name = get_sub_field('name_field');
        $title = get_sub_field('title_field');
        echo $name->name,': ',$title->name;
      }
    }
    

    for more information see
    have_rows: http://www.advancedcustomfields.com/resources/have_rows/
    taxonomy field: http://www.advancedcustomfields.com/resources/taxonomy/
    term object: http://codex.wordpress.org/Function_Reference/get_terms

  • With one small addition it worked:

    if (have_rows('repeater_field')) {
      while (have_rows('repeater_field')) {
        the_row();
        $name = get_sub_field('name_field');
        $title = get_sub_field('title_field');
        echo $name->name,': ',$title[0]->name;
      }
    }

    Each name may have multiple titles, so would there have to be another loop within to get all of those?

  • Yes, there would need to be something if a taxonomy field can have multiple selections, I missed that in your original post

    
    if (have_rows('repeater_field')) {
      while (have_rows('repeater_field')) {
        the_row();
        $name = get_sub_field('name_field');
        $titles = get_sub_field('title_field');
        echo $name->name,': ';
        $count = 0;
        foreach ($titles as $title) {
          $count++;
          if ($count > 1) {
            echo ', ';
          }
          echo $title->name;
        }
        echo '<br />';
      }
    }
    
  • Nice, that’s very close to where I got to. Thanks so much!

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

The topic ‘Multiple Taxonomies in a Repeater?’ is closed to new replies.