Functions get_field() and the_field()

Learn how to display custom fields values in your templates.

Modern Fields provides 2 functions to get or display values in your PHP files :

  • ModernFields()->fields->getField() to get a value,
  • ModernFields()->fields->theField() to display a value.

The names of the fields are automatically defined when you insert blocks into your editor, but you can change the auto generated name right away on the right sidebar.

A name holds letters, digits, hyphens and underscores, and keeps its case as typed, as ACF users write them: team-lead, jobData_descriptionContent. Two names differing only by case are the same name, since the database does not tell them apart.

Additionally, you can activate shorthand functions get_field() and the_field() in the Modern Fields settings panel.

If ACF plugin is enabled, these functions will be automatically disabled in Modern Fields to avoid conflicts. When you move a site from ACF, the ACF Migration of Modern Fields Pro switches them on as it deactivates ACF.

Show field value in PHP

You can use the function theField() anywhere in your templates and custom blocks. Replace the first param my_field by the name of your field.

PHP

For example in a paragraph.

PHP

Get field value in PHP

You can also retrieve a value instead of displaying it.

PHP

Enable shorthand functions

Go to Modern Fields > Settings from your admin dashboard. Enable shorthand functions and save.

Now, you can use the Shorthand functions in your PHP files like this:

PHP

Get an image from the ID

Modern Fields always return the raw value from the database, to guarantee the best performances possible.

If you need an image, WordPress already have built-in functions to get all the informations you need from the ID returned by Modern Fields.

PHP

Check the official documentation to learn more about WordPress built-in functions like wp_get_attachement_image() and wp_get_attachment_metadata().

We recommend using wp_get_attachment_image() because WordPress will automatically generate the image srcset for responsive.

Getting values from options, taxonomies and users

The main and shorthand functions have a second parameter to get specific values as :

  • From a specific post by providing its id
  • From an option page
  • From a taxonomy
  • From a user

Here are some examples:

PHP

For taxonomies and users, you have to add the ressource id at the end of the value, for example user_12.

Helper functions for specific fields

get_field() returns the raw value of a field. For some fields, a helper turns that value into something ready to print. These functions are always available, with no option to enable.

FunctionFieldReturns
modern_fields_get_link($value)Link, Post or Archive LinkOne link: an array with url, title and target, or null
modern_fields_get_links($value)Post or Archive LinkEvery link the field holds
modern_fields_get_icon($value, $args)IconThe markup of the icon
modern_fields_the_icon($value, $args)IconDisplays the icon
modern_fields_get_map_url($value)MapA link to Google Maps or openstreetmap.org, or null
modern_fields_get_linked_posts($field_name, $post_id)Post Reference, RelationThe IDs of the posts pointing at a post, through a field a Reverse Relation field names as its source
Available since Modern Fields 1.4.0

All but the last one take a value, not a field name: pass them the result of get_field(). On the post being pointed at, get_field() on the name of a Reverse Relation field returns the same IDs as modern_fields_get_linked_posts().

PHP

Filter the display value

Modern Fields only returns the raw value stored in the database, to guarantee the best performances possible.

That said, you can change the output value using the modern-fields/get_field/value filter.

PHP

You can learn more about Modern Fields filters here.


Hooks & Filters

Sync with theme