Knowledge Base

ACF Image field as post (cpt) image

in Ajax Search Pro Troubleshooting Tags: acfadvanced custom fieldsfunctionimageresults

In case you are using Advanced Custom fields plugin and want to use an image field as the result image, you will need a piece of additional code to make it work.

The ACF image field stores the image ID’s, not the actual URL. To detect and parse these images, you need to put the following code into your themes functions.php file:

add_filter( “asp_result_image_after_prostproc”, “asp_cf_image”, 1, 1 );

function asp_cf_image( $image ) {
if ($image != “” && strlen($image) < 10) { $atts = wp_get_attachment_image_src( $image ); if (isset($atts[0])) return $atts[0]; return null; } return $image; }