简体   繁体   English

获取第一个/一个$ item-> image JOOMLA

[英]Get first/one $item->image JOOMLA

I have a problem on my Joomla website. 我的Joomla网站上有问题。 This is the code I am having some trouble with: 这是我遇到麻烦的代码:

 echo '<div style="background: url(/images/'.$item->image.');"></div>';

This is the URL I get if my article only has one image inside: 如果我的文章中只有一个图像,这就是我得到的URL:

 <div style="background: url(/images/my_image.jpg);"></div>

This is the URL I get if my article has multiple images inside: 如果我的文章中包含多个图像,这就是我得到的URL:

 <div style="background: url(/images/my_image.jpg my_image2.jpg);"></div>

Both images are being picked up even if I only need one. 即使我只需要一张,这两个图像也会被拾取。

you can do it with the is_array function: 您可以使用is_array函数:

if(is_array($item->image)) {
 echo '<div style="background: url(/images/'.$item->image[0].');"></div>';
} else {
 echo '<div style="background: url(/images/'.$item->image.');"></div>';
}

Use the explode function to make the string an array, and then access the first item in the array… 使用explode函数使字符串成为数组,然后访问数组中的第一项…

$imageArray = explode(" ", $item->image);
echo '<div style="background: url(/images/'.$imageArray[0].');"></div>';

This of course assumes that if there are multiple images, you always want the first. 当然,这假设如果有多个图像,则始终需要第一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM