get_sub_field_object()

Overview

Returns the settings (array) of a specific sub field within a have_rows() loop.

The field’s value will also be loaded unless otherwise specified.

Changelog

  • Added in version 4.0

Parameters

get_sub_field_object($selector, [$format_value], [$load_value]);
  • $selector (string) (Required) The field name or field key
  • $format_value (bool) (Optional) Whether to apply formatting logic. Defaults to true
  • $load_value (bool) (Optional) Whether to load the field’s value. Defaults to true

Return

This function will return an array looking something like the following. Please note that each field contains unique settings.

array(
	'ID'				=> 0,
	'key'				=> '',
	'label'				=> '',
	'name'				=> '',
	'prefix'			=> '',
	'type'				=> 'text',
	'value'				=> null,
	'menu_order'		=> 0,
	'instructions'		=> '',
	'required'			=> 0,
	'id'				=> '',
	'class'				=> '',
	'conditional_logic'	=> 0,
	'parent'			=> 0,
	'wrapper'			=> array(
		'width'				=> '',
		'class'				=> '',
		'id'				=> ''
	)
);

Usage

The get_sub_field_object() function works in a similar way to get_sub_field(). This means it must be used within a have_rows() 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( have_rows('repeater') ): ?>

	<?php while( have_rows('repeater') ): the_row(); ?>

		<?php 

		// vars
		$select = get_sub_field_object('select');
		$value = $select['value'];

		?>
		<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.