Skip to content

Instantly share code, notes, and snippets.

@antishow
Last active August 2, 2018 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antishow/132c953bfeead387b248e6866f7c1514 to your computer and use it in GitHub Desktop.
Save antishow/132c953bfeead387b248e6866f7c1514 to your computer and use it in GitHub Desktop.
Code to populate the Card Style field
<?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