I added this line in my custom template to display the image:
<?php echo $article->images; ?>
And I got this as a result:
So, I tried to display the image using this:
<img src="<?php echo $images->image_intro; ?>" >
The output is this:
<img src=""> instead of
Where am I going wrong?
The image is part of a JSON encoded string, which you saw by echoing
Therefore, before trying to display the image, you'll need to decode the string first, like so:
$images = json_decode($article->images);
You're very close. You are just missing the step of decoding the
$article->images json string.
Like "A. Snake" correctly said, you have to convert the JSON string into an object by which you can access each key and value.
So this will work:
$images = json_decode($article->images); echo $images->image_intro
Or you can perform it in a single line within your tag like this:
<img src="<?php echo json_decode($article->images)->image_intro; ?>" >
To write a variable declaration using the decoded the JSON string, use the following syntax:
$value = $JSON_object->key
See the php manual for more specifications and examples.