Dynamic image source

I need to display an ACF image field via the php function in the dynamic control, but I’m not sure I understand the format to get it.
The field is in a post pointing at by a parameter in the URL E.G. //http:…com/?post_id=123

So Im working on a snippet as follows:

$passed_postId = “”;
if ( isset($_GET[‘photo_id’])) {
$passed_postId = $_GET[‘photo_id’];
if ( get_field( ‘featured_image’, $passed_postId ) ) :
echo the_field( ‘featured_image’, $passed_postId );
else :
echo “No Image”;
endif;
else {
echo “No Post_ID in URL”;
}
endif;
}

however I don’t know how I’d implement this in the dynamic field in the builder

So investigating further, I found an example that used {echo:a_function()}, so I tried the following in the dynamic content field
{echo: getphoto( {url_parameter:photo_id} )}

with my function as

function getphoto($passed_postId) {

if ( get_field( 'featured_image', $passed_postId ) ) : 
        return the_field( 'featured_image', $passed_postId ); 
else :
		return "No Featured Image";
endif;

}

didn’t work, then I tried

{echo: get_the_post_thumbnail( {url_parameter:photo_id}, ‘large’ )}

still nothing. I tried hust using {url_parameter:photo_id} in a text element, and it shows the url parameter properly, so I’m not sure why I cannot get this to work.

I haven’t found any documentation about using dynamic data in an image widget to try.

So again, I’m getting closer. I found a warning that to use the {echo:} I had to declare the function names that I want to use. OK, at least it’s being called now, but not returning an image!

OR so I thought, it definately was called and then it’s no longer being called. I don’t understand why or how to debug it other than with echos

Hi

I modified your code (replace ‘bild_fur_kopf’ with the name of your ACF image field):

function getphoto() {
$passed_postId = htmlspecialchars($_GET["post_id"]);
if ( get_field( 'bild_fur_kopf', $passed_postId ) ) : 
    $image = get_field( 'bild_fur_kopf', $passed_postId );
     return esc_url($image['url']); 
     else :
		return "No Featured Image";
endif;
}

To use it, use an image element from Bricks and enter

{echo:getphoto()}

into the dynamic field like this:

The advantage of this is that you can use the normal image element functions of Bricks to format your image.

Cheers

Patric

By the way, the return format of the ACF image field must be image-array:

Thanks Patric,
I just don’t know why it’s not returning the featured image (yes I’ve checked the post has a featured image)

I’ve even echo’d out the post title, and it’s correct

function getphoto() {
$passed_postid = htmlspecialchars($_GET[“photo_id”]);
echo get_the_title($passed_postid); <<<<<<<<<<<<<<this works
if ( get_field( ‘featured_image’, $passed_postid ) ) :
$image = get_field( ‘featured_image’, $passed_postid );
return esc_url($image[‘url’]);
else :
return “No Featured Image”;<<<<<<<<<<but I always get this!
endif;
}

well, you want the featured image or an ACF image? your mistake is that the get_field function is for retrieving ACF fields. The post thumbnail is a standard wordpress field and you cannot retrieve it with the get_field function.

You wrote about the ACF image in your post… that’s why I made you the code for an ACF image field.

This is the code to get the Thumbnail:

function getphoto() {
if(isset($_GET['post_id'])){
$passed_postId = htmlspecialchars($_GET['post_id']);
$image = get_the_post_thumbnail_url($passed_postId);
if ( !empty($image) ) : 
     return $image; 
     else :
	 return "No Featured Image";
endif;
}
}

Cheers

Patric

Sorry Partic, I’ve called the field “image” in the ACF field group, of type “featured_image”.

Thanks again, it’s working now.

Regards
Pete