I’ve looked at the documentation to create dynamic fields here: https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
Been playing around with it for awhile, but with no luck. Here’s what Im trying to do:
1. I have a Repeater field named ‘slides’
2. Within that repeater field, I gave a Group sub field named ‘templates’
3. Within that sub field, I have a Group sub field named ‘default’
4. Lastly, within the default sub field, I have a Radio field named ‘image_size’
What I’m trying to is dynamically add the available WP image sizes in that field. I need help on customizing the default sub field options.
Hope that all makes sense
It doesn’t matter in which level of the repeaters the field is placed.
Try this code to get the image sizes as options for your radio field:
<?php
function acf_load_image_sizes_field_choices( $field ) {
global $_wp_additional_image_sizes;
$field['choices'] = array();
foreach ( get_intermediate_image_sizes() as $size ) {
if ( in_array( $size, array('thumbnail', 'medium', 'medium_large', 'large') ) ) {
$width = get_option( "{$size}_size_w" );
$height = get_option( "{$size}_size_h" );
$crop = (bool) get_option( "{$size}_crop" );
} elseif ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
$width = $_wp_additional_image_sizes[ $size ]['width'];
$height = $_wp_additional_image_sizes[ $size ]['height'];
$crop = $_wp_additional_image_sizes[ $size ]['crop'];
}
$image_crop = "";
if ( $crop == 1 ) {
$image_crop = " - cropped";
}
// set the value for field values and labels as you like
$value = $size;
$label = $size . " ({$width}x{$height}{$image_crop})";
$field['choices'][ $value ] = $label;
}
// add full size option
$field['choices'][ 'full' ] = "Full size";
return $field;
}
add_filter('acf/load_field/name=image_size', 'acf_load_image_sizes_field_choices');
?>
This filter works for the field named image-size
.
I hope you get the idea how this works and you can adjust it to your needs.
Regards
Thomas
The topic ‘Dynamically add options’ 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.