Support

Account

Home Forums General Issues get_field / get_sub_field conditional?

Solved

get_field / get_sub_field conditional?

  • Hi!

    I wasn’t sure what to search for. I searched various things but didn’t really find anything that related to my situation.

    I created a component field group. It takes an image, title, blurb, URL. I call it… dun dun dun duuuunn Image Block. :: bows ::

    This component is cloned in various other field groups. Some is nested within a flex content or some are just in the normal spot.

    I created a template partial for this block and would like to use it across the board, however in some cases… the fields are a sub field and other times, its just a field.

    Right now, I have a conditional checking for the title field, since it’s required.

    if ( get_field( 'image_block_headline' ) ) {
     $title = get_field( 'image_block_headline' );
     $image = get_field( 'image_block_image' );
    } elseif ( get_sub_field( 'image_block_headline' ) ) {
     $title = get_sub_field( 'image_block_headline' );
     $image = get_sub_field( 'image_block_image' );
    }

    It’s a bit sloppy in my opinion, but it does the job. I was wondering if anyone else came across this situation and what they have done. Or if there’s some ACF trick that I’m not aware of.

    Thanks!

  • Too late to update the title, but perhaps “Detecting if its a sub field or not” would be good too…

  • @nyrngrs24 I have done this with cloned fields, what I do is that I encapsulate the cloned field in group fields. Basically I create a group field and then I add one sub field which is the clone. This means that the fields are always sub fields.

    However, my purpose for doing this was so that I could add the same clone multiple times and the group field is named differently each time so it does require different have_rows() loops.

    I get around this by getting the entire group in my template and then calling a function with the array value returned.

    
    $args = get_field('group-field-name');
    my_function($args);
    
  • An old post, back to life!

    My solution for this is a function.

    function get_acf_field( $name, $id = null ) {
        $field = get_sub_field( $name, $id ) ? get_sub_field( $name, $id ) : get_field( $name, $id );
        return $field;
    }

    Seems to work for my needs. Hopefully it’ll help others. This gives me the ability to use the same component within a repeater/flex content or standalone on a page/post. @hube2 Your idea would also be a good solution so that the component fields are always a sub field. That makes sense too. Probably a bit faster too than doing the conditional check for every field.

  • I probably read your question when you first posted it but did not have an idea of a solution at the time. The recent spam comment caused your topic to move to the top of the list. I’ve just built a site, or I should say a few sites, where I have a “CTA” block that can be added to just about anything and I needed to find a way to do that.

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

The topic ‘get_field / get_sub_field conditional?’ is closed to new replies.