the_field()
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().
Uses: get_field()
Hooks from the function
Returns
null. Displays data on screen.
Usage
the_field( $selector, $post_id, $format_value );
- $selector(string) (required)
- The field name (
nameparameter) 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. |