Home › Forums › General Issues › Display field groups based on a selection field from another field group
I have a field group on every page where you can select a template. This field group contains a selection field which I call “Templates”. Based on the the selected field I want to show a different field group.
I created a new selection field (conditional logic) for fields groups.
Step 1: Create new conditional logic for field groups
// Add new selection based on template choice in page settings (page settings is a field group displayed on every page)
add_filter('acf/location/rule_types', 'acf_location_rules_types');
function acf_location_rules_types( $choices )
{
$choices['New pages templates selection']['cpt_parent'] = 'Page templates';
echo "<pre>";
print_r($choices);
echo "</pre>";
return $choices;
}
Step 2: Get the choices from the select field of the different field group as the result of the conditional logic
Is the foreach part missing here? It shows the values in the field after “is equal too”… So I think it is good.
// Get the choices from page settings - choose your template option
add_filter('acf/location/rule_values/cpt_parent', 'acf_location_rules_values_cpt_parent');
function acf_location_rules_values_cpt_parent( $field )
{
$field_key = 'field_56cf77a4e72d3'; // Selection field inside field group "Page settings"
$field = get_field_object($field_key);
if($field) {
echo "<pre>";
print_r($field);
echo "</pre>";
}
return $field['choices'];
}
Step 3: Match the selection with the field group
— Something goes wrong here
I want to match the group field with the selection field on the page.
So if the select field is set on “page template 2” only group field 2 should be shown.
Any help is welcome!
// Match field group on the page to show it (or not)
add_filter('acf/location/rule_match/cpt_parent', 'acf_location_rules_match_cpt_parent', 10, 3);
function acf_location_rules_match_cpt_parent( $match, $rule, $options )
{
$value = get_field('pp_choose_template');
if($rule['operator'] == "==")
{
$match = ( $value == 0 );
}
elseif($rule['operator'] == "!=")
{
$match = ( $value != 1 );
}
return $match;
}
If you want to use the get_field() function outside The Loop, you need to provide the second parameter with the post ID. This page should give you more idea about it: http://www.advancedcustomfields.com/resources/get_field/.
You should be able to do it like this:
$value = get_field('pp_choose_template', $options['post_id']);
But it means that you need to save the “pp_choose_template” field first before the rule is applied.
I hope this makes sense.
The topic ‘Display field groups based on a selection field from another field group’ is closed to new replies.
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.