I'm using Gravity Forms to create several checkbox sections on my form each pre-populated with a specific taxonomy term from a custom post type.
I found an example of how to do this on the Gravity Forms site: https://www.gravityhelp.com/documentation/article/dynamically-populating-drop-down-fields/ and have modified it work as a checkbox and show the post thumbnails, but the example only works for a single checkbox.
The only way I know how to have this work for all the site taxonomies is to copy/paste the function and modify. This results in a bloated functions.php file. I figure there may be a way to do a 'foreach' loop for each of the taxonomy terms, but I am not good with foreach loops and have no idea how it would be constructed.
This is an example of the code I have copy/pasted for each instance of the taxonomy term:
add_filter( 'gform_pre_render_3', 'list_diesel_heater' );
add_filter( 'gform_pre_validation_3', 'list_diesel_heater' );
add_filter( 'gform_pre_submission_filter_3', 'list_diesel_heater' );
add_filter( 'gform_admin_pre_render_3', 'list_diesel_heater' );
function list_diesel_heater( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'checkbox' || strpos( $field->cssClass, 'diesel-heater' ) === false ) {
continue;
}
$posts = get_posts( 'numberposts=-1&post_type=product&product_cat=diesel-heaters' );
$choices = array();
foreach ( $posts as $post ) {
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
$url = $thumb['0'];
$choices[] = array( 'text' => '<img src="' . $url . '"><p>'. $post->post_title .'</p>', 'value' => $post->post_title );
}
$field->placeholder = 'Select a Post';
$field->choices = $choices;
}
return $form;
}
I was hoping it would be possible to have a foreach loop within the function and have the instances of 'diesel-heater' replaced with the taxonomy term. I am not good with foreach loops and was hoping someone could please provide some guidance.