I have been using ACF Groups to build modular websites. The idea is that each Field Group is a module and in the theme folder I have a template file/folder for each group. I can load scripts, styles, and any other settings based on the group key.
Everything works, but there’s one big limitation…
A field group can only show up once per page.
To circumvent this, I would need to duplicate that field group. But eventually duplicating field groups can cause a lot of redundancy, as a duplicated group still uses the same template file, and the same settings… just a different key.
I have been looking at other functions available in the plugin, and also at how the local json files are handled. I figured that I could define my own groups as an object or array and then use a function like:
acf_write_json_field_group($field_group) to create my own json files on the fly. And then use the sync method to automatically sync those groups without using the UI.
It looks like this could work, but I am wondering if there are better approaches to what I am trying to accomplish?
Ideally, I would like to skip the json file and just generate these groups from an object, which would allow me to create field groups on the fly, by passing different parameters as needed.
Any ideas? Suggestions?
Another approach I have used in the past, is to make one big field group with one flexible content field, and then each acf layout would correspond to a template file. But that means that all pages would start without any fields, and I am not in the business of making themes where the user has an overwhelming amount of options (which imo it makes for bad usability).
I believe the flexible content would be the best choice for this kind of situation. You can also create a field group to show the initial fields and use the flexible content if the users need to add additional modules.
If you want to use the JSON method, I suggest you use the PHP method instead. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/register-fields-via-php/. That way you don’t need to create and sync any field group.
I hope this makes sense 🙂
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2023 Advanced Custom Fields.