Using variables with acf_editor

I’m setting up a shortcode that needs to output a different field on different pages, but is otherwise the same. So rather than writing multiple shortcodes, I’m just using one with a local variable.

For example:

[template name=my_shortcode what_field=use_this_field]

And then within the template:

<Field name="{Get local=what_field}" />

This works perfectly for normal fields, but I’m having a bit of an issue when it’s an acf_editor field. Or, rather, I can’t figure out how to include the acf_editor reference so that the formatting is retained.

Is there any way to do that?

Hi @baldidiot,

Yes, you can. With the acf_ attributes the attribute value is the field name, so you can pass your variable straight into it:

<Field acf_editor="{Get local=what_field}" />

That gives you the ACF editor formatting (paragraphs etc.), whereas <Field name="..." /> returns the raw saved value.

One thing to watch out for: use acf_editor instead of name, not alongside it. If both are on the same tag, name wins and the formatting is skipped.

If the same shortcode needs to output both editor fields and regular fields, you can pass a second variable and switch on it:

[template name=my_shortcode what_field=use_this_field field_type=editor]
<If check="{Get local=field_type}" value=editor>
  <Field acf_editor="{Get local=what_field}" />
<Else />
  <Field name="{Get local=what_field}" />
</If>

Hope that helps!

1 Like