Home › Forums › General Issues › Optimising/reducing repetition across lengthy if/elseif statement › Reply To: Optimising/reducing repetition across lengthy if/elseif statement
I cannot tell what you need help with by looking at the code you provided, it seems incomplete. I assume you did not include everything because there would be a lot of code and I appreciate that, but it’s hard to tell from what was provided.
The best that I can do is tell you what I do when I have to use a single component in multiple places.
The first thing is that I try to use the same field group in all of these locations so that I can use all of the same field names. The problems is that with options pages that you need to have a way to separate the different options pages.
When setting up an options page you can set the post_id that will be used for the fields on the options page. What is not well known is that you can set this to a text value. So for example
Options page for 'vacancies_archive'
the post ID for this options page could be set to 'vacancies_archive_options'
and then the post ID for 'new_cars_archive'
could be set to 'new_cars_archive_options'
. What happens here is that the "option_name"
value of these fields in the options page are prepended with this post ID value. ACF normally uses "options"
so that fields in the options table have names that are constructed as "options_{$field_name}"
. By using a different value for the post ID you end up with "vacancies_archive_options{$field_name}"
and "new_cars_archive_options{$field_name}"
however, you should try to keep these post ID “names” short because there is a limit on the length of "opion_name"
in the DB. I am only using these as examples. I would probably use something simple like "vacancy_options"
or "new_car_options"
.
Doing this makes it possible to use the same field names because
if (have_rows('repeater_name', 'vacancy_options'))
and
if (have_rows('repeater_name', 'new_car_options'))
refer to 2 completely different fields even if the repeater field name is the same.
With that in place you simply need to figure out what “post ID” to use. For this I would create a function that looks at the queried object and then returns the correct ID.
Then I would use a template part for the "hero_section"
. This template part would call the function that gets the ID and then loops over the correct repeater using that post ID.
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.