Show intro image in Category List

by James   Last Updated July 23, 2018 14:10 PM

I added this line in my custom template to display the image:

<?php echo $article->images; ?>

And I got this as a result:

{"image_intro":"images\/articles\/liang2.jpg","float_intro":""}

So, I tried to display the image using this:

<img src="<?php echo $images->image_intro; ?>" >

The output is this: <img src=""> instead of <img src="images/articles/liang2.jpg">

Where am I going wrong?



Answers 2


The image is part of a JSON encoded string, which you saw by echoing $article->images;.

Therefore, before trying to display the image, you'll need to decode the string first, like so:

$images = json_decode($article->images);
A. Snake
A. Snake
December 02, 2015 03:27 AM

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.

PHP's manual page for the json_decode() function

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.

trajo
trajo
December 02, 2015 11:27 AM

Related Questions


How to assign template to a component?

Updated February 05, 2018 21:10 PM

Using tmpl=component with SEF URL

Updated July 03, 2017 07:10 AM



Different template for auto generated component page

Updated August 31, 2016 08:04 AM