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.setting
Used for getting the setting of an ACF Field, for example its default value.
Examples
Fetch a Field from a Specific Post
{acf:price@id=15}
This shortcode retrieves the
price
field from the post with ID 15.Get User-specific Field Data
{acf:bio@user}
This example pulls the
bio
field for the current logged-in user.Retrieve an Options Field Value
{acf:color@option}
This will get the value of the
color
options field.Access Term-related Field Data
{acf:ingredients@term id=5}
Here, the shortcode fetches the
ingredients
field from the term with ID 5.Obtain a Field with ACF Formatting
{acf:gallery@format}
This example returns the
gallery
field with ACF's formatting applied.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.
Special Privileges
Certain operations, such as accessing private data or specific user fields, necessitate the use of Power Shortcodes, which possess comprehensive privileges.