Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Questions about plugin setup / features › Reply To: Questions about plugin setup / features
February 21, 2020 at 4:21 pm
#25931
Keymaster
Hi,
3. Well, we can actually try a modification to the previous code:
add_filter( 'asp_results', 'asp_get_main_prod_img', 10, 1 );
function asp_get_main_prod_img($results) {
$acf_image_field = 'image_field_name';
$size = 'single-post-thumbnail';
foreach ( $results as $k => &$r ) {
if ( isset($r->post_type) && $r->post_type == 'product_variation' && empty($r->image) ) {
$wc_prod_var_o = wc_get_product( $r->id );
$img = wp_get_attachment_image_src(
get_post_thumbnail_id($wc_prod_var_o->get_parent_id()),
'single-post-thumbnail'
);
if (isset($img, $img[0]) && !is_wp_error($img))
$r->image = $img[0];
}
if ( empty($r->image) && function_exists('get_field') ) {
$image_id = get_field($acf_image_field, $r->id);
if ( !empty($image_id) ) {
$img = wp_get_attachment_image_src(
$image_id,
'single-post-thumbnail'
);
if (isset($img, $img[0]) && !is_wp_error($img))
$r->image = $img[0];
}
}
}
return $results;
}
Important:
– Change the $acf_image_field and the $size variables at the beginning of the function
– Remove the custom field source from the image sources – so that the code can handle those images programatically