ACF - Dynamic Shortcodes
Updated this week

An ACF (Advanced Custom Fields) shortcode allows you to fetch an ACF field to display in a template. To use a Dynamic Shortcode, you simply pass the meta as the first argument. For example, you can use {acf:field_id} to retrieve a field associated with the current page.

Explicitly Setting the ID of a WordPress Object

If you need to explicitly set the ID of the WordPress object (e.g. a post or page) from which you want to get the field, you can add the @id attribute to the shortcode. For example {acf:field_id@id=123} to represent the post with an ID of 123.

Using the Format Keyarg

If you want to use ACF formatting for the field, you can use the @format keyarg. For example, {acf:my_date@format} for a date field will output the format as set in the field settings.

ACF Fields Locations

Besides posts and pages ACF allows you to insert field on other WordPress locations, such as the options page, the user page and the term page. If your field is not located on a page or post you need to specify the location in order to fetch it. You can do this with one of the following keyargs: user, option and term. With the user location if you don't specify the ID the fields will refer to those of the currently logged in user.

ACF Repeater and Flexible Content

There are two ways to get data from the ACF Repeater and Flexible Content fields.

The more simple, but less flexible, way is to use the acf-loop shortcode. This uses internally the standard ACF looping functions. The other option is to use the normal acf shortcode, which for this kind of field returns an array that can be looped through, or in which single items can be accessed with the accessor filter.

ACF Loop

Pass as the first argument the name of the field and as second the template that should be printed at each iteration. Say we have a pizza repeater with a subfield called name and one quantity. Than you can print them all in a list with the following shortcode {acf-loop:pizza-toppings [<li>name: {acf:name@sub} qty: {acf:quantity@sub}]}. You can get subfields with the sub keyarg as explained in the next paragraph.

Subfields in ACF Loop

If you want to fetch the value of a subfield inside an ACF Loop you can use the sub keyarg, for example {acf:sub_field_id@sub}. Notice that this is not compatible with the ID keyarg.

Using the normal acf shortcode with a Repeater or Flexible Content

If you don't want to loop through all the rows of an ACF Repeater but access only some of them you can use the normal acf shortcode: {acf:repeater-field-name}. This will return an array of rows, each row will contain its subfield as an associative array. You can then use the accessor filter to retrieve the subfields you are interested too.

Did this answer your question?