Support

Account

Home Forums General Issues Many of the same clone in another set of fields

Solved

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.

    example field group

    example with two clones

    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>
    
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.