acf_walk_select_input()ACF 5.6.0

acf_walk_select_input

Returns the HTML of a select input's choices.

Хуков нет.

Возвращает

Строку.

Использование

acf_walk_select_input( $choices, $values, $depth );
$choices(массив)
The choices to walk through.
По умолчанию: array()
$values(массив)
The selected choices.
По умолчанию: array()
$depth(массив)
The current walk depth.

Список изменений

С версии 5.6.0 Введена.

Код acf_walk_select_input() ACF 5.10.2

function acf_walk_select_input( $choices = array(), $values = array(), $depth = 0 ) {
	$html = '';

	// Sanitize values for 'selected' matching (only once).
	if ( $depth == 0 ) {
		$values = array_map( 'esc_attr', $values );
	}

	// Loop over choices and append to html.
	if ( $choices ) {
		foreach ( $choices as $value => $label ) {

			// Multiple (optgroup)
			if ( is_array( $label ) ) {
				$html .= sprintf(
					'<optgroup label="%s">%s</optgroup>',
					esc_attr( $value ),
					acf_walk_select_input( $label, $values, $depth + 1 )
				);

				// single (option)
			} else {
				$attrs = array(
					'value' => $value,
				);

				// If is selected.
				$pos = array_search( esc_attr( $value ), $values );
				if ( $pos !== false ) {
					$attrs['selected'] = 'selected';
					$attrs['data-i']   = $pos;
				}
				$html .= sprintf( '<option %s>%s</option>', acf_esc_attr( $attrs ), esc_html( $label ) );
			}
		}
	}
	return $html;
}