Hi guys,
I have been brainstorming about an idea but have not figured out a path about how to implement the same. So, I am explaining the situation here in the hope someone can show me some path to figure out how to implement it.
Explanation of the Problem:
So, let’s say that we have a footer CTA section that shows up just above the footer. Now, the user can enter all the details needed for that footer CTA to work and it will show up. It’s simple and we all know it.
But the problem is, the user has 4/5 different templates of the footer CTA. In each case the CTA heading, description and other details are different but the CTA structure remains the same.
Now the user can definitely provide all the details manually for each page and provide whatever description, title they need. But instead, the user is asking for a way so that inside the Global Theme Settings page, they will enter all the different variations of the CTA.
So, we can definitely have an ACF Repeater field where the user can add all the different variations of the CTA (i.e. different heading, description and other stuff). Now what the user is asking is that on the individual page/post there will be a page/post settings where they can select the variation they have already entered in the theme global settings page from a dropdown and that data will automatically show up in the CTA.
Where I am stuck:
So, creating a repeater in the theme global settings page is easy. And there they can enter all the different variations on each row.
But where I am stuck is how am I able to show those repeater data in a dropdown for page/post settings? So, that in the dropdown it will show up as like Variation 1, Variation 2 and so on and whatever variation they select that correspondent data will be fetched from the Repeater.
Does anyone have any idea on how to achieve this? I’m really sucking on limbo and could not figure this out. Also didn’t find any similar help articles on the internet to guide me.
Really a huge thanks in advance for all your helps.
What if you create a repeater in the theme options.
It has title, description, link etc. But you add another column for post object. This is then a multiselect and allows the user to select which page that row should appear on. I’d set it to return the post ID
Your code then needs a line in your repeater which gets the current page ID and compares it to the row post object IDs. If it matches, show the content.
Would that work?
Yes but in this case if the user add any new pages, posts etc. then they need to come back to the theme settings again selecting under which variation that page will fall under. That is just too much back and forth and not sure how the user will react as for each page/post they need to repeat this step.
I would create a CPT to hold the CTA. Each post in the CPT would hold a single variation for a CTA. Then I would create a repeater on the page where that uses a post object field to select with CTA they want to use.
Hi John,
Yes that is a smart way of resolving that. But creating a CPT just for 5/6 CTA variation, doesn’t it feel too extreme?
Is there no way to populate a dropdown inside the page/post settings where it will show the repeater items added in the theme global settings page and then whatever the user select, get the respective data from the repeater?
I create CPTs for things like this all the time.
I have also done this with a repeater.
You will need to create some type of a ID field that will not change, cannot be edited. I use a field that has a unique ID created automatically that is set to read only.
In the select field acf/prepare_field you get the loop over the repeater and set the choice values to this id and the label to some king of title field on the row.
To show this you would need to loop over the repeater of your options page to find the matching ID for every CTA.
Thanks a lot John for your reply. After reading this, it really feels that the CPT is the simpler route. But still, do you have any example code snippet of acf/prepare_field
about how to execute this?
Now that I look at what I have. I have a class that sores the repeater in an array so I don’t need to get it every time. Here is a very simple example:
<?php
new your_class_name();
class your_class_name {
private $cta_choices = array();
public function __construct() {
add_filter('acf/prepare_field/name=your_field_name', array($this, 'choices'));
}
public function choices($field) {
if (!empty($this->cta_choices)) {
$field['choices'] = $this->cta_choices;
return $field;
}
if (have_rows('cta_repeater', 'options')) {
$choices = array();
while (have_rows('cta_repeater', 'options')) {
the_row();
$class = get_sub_field('id_field_name'); // this is my unique ID field
$text = get_sub_field('title_field_name');
$choices[$class] = $text;
} // end while have rows
$field['choices'] = $choices;
}
$this->cta_choices = $field['choices'];
return $field;
}
}
Thank you so much man for the code snippet. Really helped a lot. Thank you so much….
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!
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.