Support

Account

Home Forums ACF PRO get_field('field')['subfield'] vs get_field('field_subfield') Reply To: get_field('field')['subfield'] vs get_field('field_subfield')

  • Sorry, I didn’t mean to imply anything other than I don’t use shortcuts. I’m sure if I wanted to spend several hours stepping through the code I could figure out what it works in some cases and does not in others but I would rather not.

    To answer your question, I would likely do what I posted above

    
    // get group
    $group_value = get_field('group_field_name');
    
    // get the image
    $image = $group_value['image_sub_field_name'];
    // get the url
    $url = $image['sizes']['large'];
    
    // or I might shorten the above two lines to something like this
    $url = $group_value['image_sub_field_name']['sizes']['large'];
    

    If we really want to get down to what I would do, I would not return an image array for an ACF image field to begin with. Why? Because getting the details for every size of an image causes extra work to be done. This extra work will not be noticed for 1 image field but if you have 10 image fields all returning this information then there would likely be a performance hit. If the only image size I am going to use it “large” then I would return the image ID and then get the other information I need using built in WP functions, something like this.

    
    $group_value = get_field('group_field_name');
    // image field set to return image ID
    $image_id = $group_value['image_sub_field_name'];
    // get only the image size I need
    $image = wp_get_attachment_image_src($image_id, 'large');
    $image_url = $image[0];
    // get the alt for image
    $image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true);
    // etc...