🎉 Launch Party!

discount for all licences until July 17.

Buy now →

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.

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

If ACF plugin is enabled, these functions will be automatically disabled in Modern Fields to avoid conflicts.

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.

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