the_field()

Overview

Displays the value of a specific field.

Intuitive and powerful (much like ACF itself ?), this function can be used to load the value of any field from any location. Please note this function is the same as echo get_field( ... );

Parameters

the_field($selector, [$post_id], [$format_value]);
  • $selector (string) (Required) The field name or field key
  • $post_id (mixed) (Optional) The post ID where the value is saved. Defaults to the current post
  • $format_value (bool) (Optional) Whether to apply formatting logic. Defaults to true

Usage

Display a value from the current post

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

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

Display a value from a specific post

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

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

Check if value exists

This example shows how to check if a value exists before displaying it.

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

Display a value from other places

$post_id = null; // current post
$post_id = 1; // post 1
$post_id = "option"; // options page
$post_id = "options"; // same as above
$post_id = "category_2"; // target a specific category
$post_id = "event_3"; // target a specific taxonomy (this tax is called "event")
$post_id = "user_1"; // target a specific user (user id = 1)

the_field( "text_field", $post_id );

Related