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@keyargs}

Here, field_name refers to the desired ACF field for data extraction. The @keyargs parameter allows for additional configuration and filtering, such as specifying a post ID or opting for a particular field format.

Key Arguments

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

  • option: Indicates that the field is an options field.

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

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

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

  • subfield: Denotes a subfield within a repeater or flexible content field.

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.

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.

Special Privileges

Certain operations, such as accessing private data or specific user fields, necessitate the use of Power Shortcodes, which possess comprehensive privileges. These privileged shortcodes can perform a broader range of tasks and access restricted data.

Did this answer your question?