On this page
- Category: Functions
- Written: December 21, 2011
- Updated: December 15, 2012
get_field()
Description
Returns the value of the specified field.
This is a very versatile function. You can use it to store a value, echo a value and interact with a value. Please note the type of variable returned is relative to the field type. Eg: A repeater will return a multidimensional array.
Parameters
<?php $field = get_field($field_name, $post_id); ?>
- $field_name: the name of the field to be retrieved. eg “page_content” (required)
- $post_id: Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc
Usage
<?php /* * get a field value from the current post */ $value = get_field( "text_field" ); /* * get a field value from another post */ $value = get_field( "text_field", 123 ); /* * use get_field with conditional statements */ $value = get_field( "text_field" ); if( $value ) { } else { } /* * $post_id examples */ $post_id = null; // current post $post_id = 1; $post_id = "option"; $post_id = "options"; // same as above $post_id = "category_2"; // save to a specific category $post_id = "event_3"; // save to a specific taxonomy (this tax is called "event") $post_id = "user_1"; // save to user (user id = 1) ?>


