Home › Forums › General Issues › Many of the same clone in another set of fields
I have a field group for ‘image fields’ – that has many crops etc. So, just a field group.
I want to use that field group many times in my ‘article fields’ group.
I have them both set to display: ‘group’ – but they are essentially the same fields… so they just overwrite each other – (which makes sense – because they are ‘the same’ and are on the same level… ) – so, what is the best way to use it multiple times. Should I make a subfield – so that it’s not on the same level? I guess I’ve only ever two used them in flexible-content before.
I’m going to try a few things – but I wanted to ask with photos while I had a good example. : )
I changed the article_poster / article_hero fields to ‘group’ instead of ‘clone’ – and made a subfield called ‘image’ and then cloned that.
$poster = get_field('article_poster');
$posterSmall = $poster["small_crop"]["url"];
$posterMedium = $poster["medium_crop"]["url"];
$posterLarge = $poster["large_crop"]["url"];
but my first instant was to access [“image”] – which I didn’t need to do – because the clone just replaces that.
You can do a number of things.
When creating a clone field you can set the display to group and turn on field name prefixing.
You can create a “group” field and make the clone a sub field in the group.
You can create a repeater and make the clone a sub field of the repeater.
Ah. Field name prefixing didn’t click for me. Excellent. I see it here: https://www.advancedcustomfields.com/resources/clone
For the most part using a group or using prefixing in ACF does the same thing to the field name. But there is one difference that can effect coding.
I generally use group fields and I will explain why.
Usually I create a template part for showing content from a clone. Think of it like a component.
So let’s say have a clone with just one field in it named “field_x”
If I use clone prefixing then my code will change every time I use the clone so that I can call “prefix_1_fied_x”, “prefix_2_field_x”, etc.
If I use a group field then I can do this
if (have_rows('group_z')) {
// always true, a group field is a repeater that always has one row
while (have_rows('group_z)) {
the_row();
get_template_part('template-parts/components/my-component');
}
}
and inside the my-component.php file I can always call
get_sub_field('field_x')
no matter what group it’s in. for one or two fields maybe not a big deal, but if you have 10 or 40 fields in your clone then you only need to code a new loop to call the component template rather then re-code all of the field calls to use the new prefixed field names.
Excellent point!
I went with group / but I wouldn’t have seen that prefix side-effect until I tried it out. I don’t use template part / because I’m stubborn and it’s ugly… but my components certainly use a generic variable that needs to stay stable. I usually create a little get-example-data.php
file to organize the fields into variables so I can use them in many contexts.
<example-card>
<h2 class='title'><?=$title?></h2>
</example-card>
I tried to follow the suggestion posted by John in https://support.advancedcustomfields.com/forums/topic/many-of-the-same-clone-in-another-set-of-fields/#post-129384 so:
if (have_rows('testimonials')) {
while (have_rows('testimonials')) {
the_row();
echo get_sub_field('title');
}
}
However if I use this code
$testimonials = get_field( 'testimonials' );
all values are inside $testimonials[‘testimonials’]
Clearly I’m missing something…any clues?
@xxvii when cloning are you using Seamless or Group for the display?
Sorry, I missed that.
This means that your coned fields are in a group in a group
Testimonials (group)
=> Testimonials (clone – group)
===> sub field
to use have rows you need to loop over both groups. This is going to be an issue because your top level field and the clone group have the same name.
So your suggestion is not cloning the whole group in #2, but the single fields contained in the “source” group?
This could be a problem if the source group has many fields or if I need to add a field to the source group.
In that case I should add the new field in every place I cloned the other fields, and this is something I would avoid.
What I’m tryng to do is something similar to the flexible content field (which I can’t use for a number of reasons).
I would like to have a Field Group where I create several “modules”, where each one is a group containing some fields.
Then in other Field Groups pertaining to pages or posts or whatever, I clone the specific modules I need for that page (the whole group, not the single fields contained in it).
Also, I could need multiple instances of the same module, which is the topic of this thread.
What is the best strategy to do this?
Thank you.
No, what I am saying is that you need to treat the testimonials clone as a group. There is a difference when you use this layout in the clone field.
This is your code
if (have_rows('testimonials')) {
while (have_rows('testimonials')) {
the_row();
echo get_sub_field('title');
}
}
With the group layout on the clone your code should look something like this
if (have_rows('testimonials')) {
while (have_rows('testimonials')) {
the_row();
// add loop for clone field
// this will be an issue because your clone field has the same name as it's parent
// you will need to change the field name
if (have_rows('testimonials')) {
while (have_rows('testimonials')) {
the_row();
echo get_sub_field('title');
}
}
}
}
Oh yes, I got that, sorry.
I was asking if there’s a more succint alternative than nesting the clone group inside another group.
From what I understand there’s none, besides using prefixes, with all the disadvantages you already mentioned.
The best and most flexible way would be to use Flexible content (which is the way I usually go, but in this project I can’t)
Nevertheless, I prefer to get the values as an array using get_field on the outer group, without resorting to nested cycles.
I would just clone the field group directly into the parent group and set it to seamless, at least that is how I’ve always done it. The all of the fields in the cloned group become sub fields of the repeater row.
I don’t know of any reason to have a group in a group. As long as all of the parent group fields have a unique name there shouldn’t be any issues.
The topic ‘Many of the same clone in another set of fields’ is closed to new replies.
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 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.