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 />';
}
}
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re hard at work on ACF 6.1, and Beta 1 is now available 🚀
— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.