ryanC Posted April 18, 2017 Posted April 18, 2017 Hi, on my test site I have created a field that holds an image. I have called the field "result_pic". I want that image to show up on my search results page. I have looked over this page: https://processwire.com/api/fieldtypes/images/ and some of it makes sense but it's taking me a while to get these php concepts (no php background). Looking over the forum, I found this thread: https://processwire.com/talk/topic/15128-images-not-showing-in-search-results/ where the user had the same problem I was having of not seeing their image. One of the responses was: On 12/29/2016 at 8:10 AM, Klenkes said: I usually do it this way: if(count($match->pic)){// if pic is the fieldname and more than one image is allowed $resized = $match->pic->first->size(100,0);// if the image has to be smaller than original echo "<img src='$resized->url' width='100' alt=' '>"; } if pic has more than one image allowed in max filers setting And this worked for me, the image showed up at a smaller size. So if I didn't learn anything else, I could just leave it at that. But my question is, how do I simplify this? Can I get the image to show up without resizing it, and without putting it in a special if statement? So far I am just typing out combinations, none of which are showing the image...just a series of broken image links. Quote foreach($matches as $match) { echo "<li><a href='$match->url'>$match->title</a>"; echo "<div class='summary'>$match->summary</div></li>"; <!--result_pic image goes here--> echo "<img src='$match->$result_pic->url'>"; echo "<img src='$match->result_pic->url'>"; echo "<img src='$match->$image->page->result_pic->url'>"; echo "<img src='$match->result_pic->first()->url'>"; echo "<img src='$match->$image->url->result_pic->first()->url'>"; echo "<img src='$match->$image->page->$image->url->result_pic->first()-url'>"; echo "<li><a href='$match->$image->url'>$match->result_pic</a>"; //echo "<img src='$match->result_pic->first()->url"; this deletes the working example Can I just echo the picture in a simple way, like the $match->title, and $match->summary ? Thanks!
abdus Posted April 18, 2017 Posted April 18, 2017 (edited) @ryanC, have you set field to accept single file, or with no limits? If you've set (or didn't change anything) Max files allowed as 0, then $match->result_pic is a WireArray, if it's 1, then it's a single Pagefile. Something like this should work. <?php foreach($matches as $match) { echo "<li><a href='$match->url'>$match->title</a>"; echo "<div class='summary'>$match->summary</div></li>"; // this should work no matter what // check if result_pic is a WireArray if($match->result_pic->count) { $url = $match->result_pic->first()->url; } else { // it's a Pageimage $url = $match->result_pic->url; } echo "<img src='$url'>"; If you know what type of image field this is, then you can remove checks with if statement, and use one or the other $url and echo directly. The reason it wasn't working is that PHP expects simple expressions inside double quoted string "" for string interpolation, so only $a, or $a->b works, the remaining parts are echoed as strings. This would mean echo "<img src='$match->result_pic->first()->url'>"; would only interpolate the first $match->result_pic part, and replace it (with presumably the id of the image). For more info http://php.net/manual/en/language.types.string.php#language.types.string.parsing Edited April 18, 2017 by abdus added documentation link 3
ryanC Posted April 18, 2017 Author Posted April 18, 2017 Thanks abdus! That really helped me out. Originally I did not change the maximum files allowed, it was at the default 0. But since I only want one image for this field, I decided to change the limit to 1, and simplified the code to: Quote $url = $match->result_pic->url; echo "<img src='$url' width='50' alt=' '>"; Nice and simple. 1
abdus Posted April 18, 2017 Posted April 18, 2017 Glad to help, @ryanC 12 minutes ago, ryanC said: I decided to change the limit to 1 You do not have to set it to 1, you can also get the first image ($match->result_pic->first()->url) or use tags ($match->result_pic->getTag('some-tag')). And if you rename your field to a more generic name, you can reuse it across many templates, reducing the work DB engine has to do. 1
ryanC Posted April 18, 2017 Author Posted April 18, 2017 Thanks abdus--went back and set it back to array. I suppose limiting it would be more helpful if I was trying to limit what another user could upload to the site. I will look into tags, seems useful. And it's a good idea to not have multiple copies of the same image, so yeah a more generic name will make the pics multi-purpose, I can just resize the pic in the search results code.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now