get_sub_field_object()

Description

This function will return a sub field object (within a has_sub_field loop). The returned array will look something like this:

Array
(
    [key] => field_4fea85f5320da
    [label] => Text Field
    [name] => text_field
    [type] => text
    [instructions] => 
    [required] => 0
    [default_value] => 
    [formatting] => html
    [order_no] => 0
)

Requirements

  • ACF version 4.0.0 or above

Parameters

get_sub_field_object( $field_key )

$field_key

The key or name of the field to be retrieved. eg “field_4fc5ab37e1819lol” or ”heading” (required)

Usage

The get_sub_field_object works in a similar way to get_sub_field. This means it must be used within a has_sub_field loop. Here is an example of how to find the sub field object of a ‘select’ field (called ‘select’ in this example) from a repeater (called ‘repeater’ in this example).

<?php if( get_field('repeater') ): ?>

	<?php while( has_sub_field('repeater') ): ?>

		<?php 

		// vars
		$select = get_sub_field_object('select');
		$value = get_sub_field('select');

		?>
		<ul>
			<?php foreach( $select['choices'] as $k => $v ): ?>
				<li>
					<?php if( $k == $value ): ?>
						<span class="selected">Selected!</span>
					<?php endif; ?>
					<?php echo $v; ?>
				</li>
			<?php endforeach; ?>
		</ul>

	<?php endwhile; ?>

<?php endif; ?>

 

Related

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.