Skip to main content
ACF - Dynamic Shortcodes

Discover the ACF Dynamic Shortcode to fetch and display Advanced Custom Fields data dynamically within posts

Updated over a week ago

The ACF Dynamic Shortcode serves as a versatile tool for accessing values stored in Advanced Custom Fields within WordPress. It enables specific data retrieval from fields, including options, terms, and user data. This shortcode is particularly beneficial for developers and content managers who require dynamic content presentation in WordPress pages or posts.

Syntax

Utilize the ACF Dynamic Shortcode with the following structure:

{acf:field_name}

  • field_name refers to the desired ACF field for data extraction.

Key Arguments

  • id Defines the ID for the post, term or user from which the field data is retrieved.

    {acf:field @ID=3} default is post
    {acf:field @ID=3 post}
    {acf:field @ID=27 term}
    {acf:field @ID=25 user}

  • option Indicates that the field is an options field.

    {acf:field @option}

  • user Specifies that the field is related to a user's data.

    {acf:field @user} current user
    {acf:field @ID=53 user}

  • term Used when the field is associated with a taxonomy term.

    {acf:field @term} current term
    {acf:field @ID=72 term}

  • format Determines whether to apply ACF formatting to the field value.

    {acf:field @format}
    {acf:field @format ID=3}
    {acf:field @format ID=23 user}
  • setting Used for getting the setting of an ACF Field, for example its default value, label.

    {acf:field @setting=default_value}
    {acf:field @setting=default_value ID=3}
    {acf:field @setting=default_value ID=23 user}
    {acf:field @setting=label}
    {acf:field @setting=name}

Examples

  1. Fetch a Field from a Specific Post

    {acf:price@id=15} 

    This shortcode retrieves the price field from the post with ID 15.

  2. Get User-specific Field Data

    {acf:bio@user} 

    This example pulls the bio field for the current logged-in user.

  3. Retrieve an Options Field Value

    {acf:color@option}

    This will get the value of the color options field.

  4. Access Term-related Field Data

    {acf:ingredients@term id=5}

    Here, the shortcode fetches the ingredients field from the term with ID 5.

  5. Obtain a Field with ACF Formatting

    {acf:gallery@format}

    This example returns the gallery field with ACF's formatting applied.

  6. Get the default value of a field

    {acf:name@setting=default_value} 

    This will display the default value that was set for the field name in the ACF Fields settings.

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} qty:{acf:quantity}]
}

Subfields in ACF Loop

If you wish to retrieve the value of a subfield within an ACF loop, you can call it as a normal ACF field {acf:field_id}. Note that in this case it is not compatible with the ID keyarg.

ACF Field within a Group

If you wish to retrieve the value of a field inside a group you should use this syntax:

{acf:group_name_field_id}

The group name separated by an underscore must be entered as argument.

ACF Flexible Content fields

You can dynamically loop through the rows of an ACF Flexible Content field and conditionally render templates for each layout. The general structure is:

{acf-loop:<field_name>
{switch:{acf-row:layout}
"<layout_name>" [<template_for_layout>]
...
<default_template>
}
}
  • <field_name>: The name of the ACF Flexible Content field.

  • <layout_name>: The identifier of each layout in the Flexible Content field.

  • <template_for_layout>: The template to display for a specific layout.

  • <default_template> (optional): A fallback template for unrecognized layouts.

Example

Suppose you have a Flexible Content field called page_sections with the following layouts:

  • hero_section

  • text_block

Here’s how you can render different HTML templates for each layout:

{acf-loop:page_sections
{switch:{acf-row:layout}
"hero_section" [<section class="hero">{acf:hero_title}</section>]
"text_block" [<div class="text-block">{acf:text_content}</div>]
}
}
  • {acf:hero_title}: Fetches the ACF field hero_title for the current row.

Special Privileges

Certain operations, such as accessing private data or specific user fields, necessitate the use of Power Shortcodes, which possess comprehensive privileges.

Did this answer your question?