the_field()ACF 1.0.3

Displays the value of the specified field (meta-field).

This powerful function can be used to display the value of any field anywhere.

This is a wrapper for get_field().

Returns

null. Displays data on screen.

Usage

the_field( $selector, $post_id, $format_value );
$selector(string) (required)
The field name (name parameter) or key (id).
$post_id(integer/object)
The ID of the post where the value is stored.
Default: the current post
$format_value(true/false)
Apply formatting logic.
Default: true

Examples

#1 Display a value from the current post

This example shows how to display the value of the text_field field from the current post.

<?php the_field( 'text_field' ); ?>

#2 Display a value from a specific post

This example shows how to display the value of the text_field field from post ID 123.

<?php the_field( 'text_field', 123 ); ?>

#3 Check whether a value exists

This example shows how to check whether a value exists (is set) before displaying it.

<?php if( get_field('text_field') ): ?>
	<h2><?php the_field('text_field'); ?></h2>
<?php endif; ?>

#4 Get values from different objects

This example shows many permitted $post_id values that specify where the value is stored.

$post_id = false;           // current post
$post_id = 123;             // post ID = 123
$post_id = "user_123";      // user ID = 123
$post_id = "term_123";      // term ID = 123
$post_id = "category_123";  // same as above
$post_id = "option";        // options page
$post_id = "options";       // same as above

the_field( 'my_field', $post_id );

Changelog

Since 1.0.3 Introduced.

the_field() code ACF 6.8.8

function the_field( $selector, $post_id = false, $format_value = true ) {
	$field = get_field_object( $selector, $post_id, $format_value, true, $format_value );
	$value = $field ? $field['value'] : get_field( $selector, $post_id, $format_value, $format_value );

	if ( is_array( $value ) ) {
		$value = implode( ', ', $value );
	}

	// If we're not a scalar we'd throw an error, so return early for safety.
	if ( ! is_scalar( $value ) ) {
		return;
	}

	// If $format_value is false, we've not been able to apply field level escaping as we're giving the raw DB value. Escape the output with `acf_esc_html`.
	if ( ! $format_value ) {
		$value = acf_esc_html( $value );
	}

	// Get the unescaped value while we're still logging removed_unsafe_html.
	$unescaped_value = get_field( $selector, $post_id, $format_value, false );
	if ( is_array( $unescaped_value ) ) {
		$unescaped_value = implode( ', ', $unescaped_value );
	}

	if ( ! is_scalar( $unescaped_value ) ) {
		$unescaped_value = false;
	}

	$field_type = is_array( $field ) && isset( $field['type'] ) ? $field['type'] : 'text';
	if ( apply_filters( 'acf/the_field/allow_unsafe_html', false, $selector, $post_id, $field_type, $field ) ) {
		$value = $unescaped_value;
	} elseif ( $unescaped_value !== false && (string) $value !== (string) $unescaped_value ) {
		do_action( 'acf/removed_unsafe_html', __FUNCTION__, $selector, $field, $post_id );
	}

	echo $value; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped by logic above.
}