Last active
August 2, 2018 13:43
-
-
Save antishow/132c953bfeead387b248e6866f7c1514 to your computer and use it in GitHub Desktop.
Code to populate the Card Style field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get all layouts with the "Card Layout:" prefix | |
*/ | |
function get_card_layouts() { | |
$card_layouts = array_filter(acf_get_field_groups(), function($group) { | |
return strpos($group['title'], 'Card Layout:') === 0; | |
}); | |
return array_values($card_layouts); | |
} | |
/** | |
* Get all layouts with the "Card Layout:" prefix, but | |
* in an array with cleaned up labels ready for the Card Grid | |
*/ | |
function get_card_layouts_as_select_options() { | |
return array_reduce(get_card_layouts(), function($choices, $layout) { | |
$key = str_replace('group_', '', $layout['key']); | |
return array_merge($choices, [ | |
$key => str_replace('Card Layout: ', '', $layout['title']), | |
]); | |
}, []); | |
} | |
/** | |
* Wire up the Card Grid style choices to get_card_layouts() | |
*/ | |
add_filter('acf/load_field/key=field_card_grid_style', function($field) { | |
return array_merge($field, [ | |
'choices' => get_card_layouts_as_select_options(), | |
]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment