Jon E Posted July 19, 2012 Share Posted July 19, 2012 Hi, Sorry for the very basic question, I have looked here and tried to find an answer but I had no luck. Basically I would like to output the first image for each 'item' featured (they all have an image field titled 'images' in the standard way). So, my code so far is: <?php $features = $pages->find("featured=1, limit=5, sort=-date"); foreach($features as $feature) { echo "<li>" . "<h3><a href='{$feature->url}'>{$feature->title}</a></h3>" . "<em>{$feature->date}</em>" . "<p>{$feature->intro}</p>" . "</li>"; } ?> Would anybody be able to help me to ouput the first image from the images field for each of these? Again, sorry for such a basic question. I am so impressed with Processwire, I have used it for 4 sites now and would return again and again. There's nothing that comes close out there. Thanks, Jon 1 Link to comment Share on other sites More sharing options...
Pete Posted July 19, 2012 Share Posted July 19, 2012 No probs - no such thing as a bad question. You'll want $feature->images->first()->url and if you want the description too then same as this but description instea of url Link to comment Share on other sites More sharing options...
Jon E Posted July 19, 2012 Author Share Posted July 19, 2012 Thanks very much, that works a treat. If I wanted to then resize the image to a square how could I include this? So my code is: <?php $features = $pages->find("featured=1, limit=5, sort=-date"); foreach($features as $feature) { echo "<li>" . "<img src='{$feature->images->first()->url }' width='180px'>" . "<h3><a href='{$feature->url}'>{$feature->title}</a></h3>" . "<em>{$feature->date}</em>" . "<p>{$feature->intro}</p>" . "</li>"; } ?> ...and I want to include a function like: $thumb = $image->size(325,325); Again, thank-you so much - answers to questions like these make it so easy to pick things up. Link to comment Share on other sites More sharing options...
Pete Posted July 19, 2012 Share Posted July 19, 2012 Just after your foreach I would do $thumb = $feature->images->first()->size(325,325); and then you can output $thumb->url and description as normal. Link to comment Share on other sites More sharing options...
Jon E Posted July 19, 2012 Author Share Posted July 19, 2012 Hi, I've put the code: <?php $features = $pages->find("featured=1, limit=6, sort=-date"); foreach($features as $feature) { $thumb = $feature->images->first()->size(100,100); echo "<li>" . "<a href='{$feature->url}'><div id='explore_thumb'><img src='{$feature->images->first()->url }'></div></a>" . "<h3><a href='{$feature->url}'>{$feature->title}</a></h3>" . "<em>{$feature->date}</em>" . "<p>{$feature->intro}</p>" . "</li>"; } ?> Seems not to work though? Thanks I really appreciate this. Link to comment Share on other sites More sharing options...
Soma Posted July 19, 2012 Share Posted July 19, 2012 It should be then $thumb->url; Link to comment Share on other sites More sharing options...
Jon E Posted July 19, 2012 Author Share Posted July 19, 2012 It should be then $thumb->url; In what context? Link to comment Share on other sites More sharing options...
interrobang Posted July 19, 2012 Share Posted July 19, 2012 I updated your code: <?php $features = $pages->find("featured=1, limit=6, sort=-date"); foreach($features as $feature) { $thumb = $feature->images->first()->size(100,100); echo "<li>" . "<a href='{$feature->url}'><div id='explore_thumb'><img src='{$thumb->url}'></div></a>" . "<h3><a href='{$feature->url}'>{$feature->title}</a></h3>" . "<em>{$feature->date}</em>" . "<p>{$feature->intro}</p>" . "</li>"; } ?> 1 Link to comment Share on other sites More sharing options...
Pete Posted July 19, 2012 Share Posted July 19, 2012 Yup, what they said Link to comment Share on other sites More sharing options...
Jon E Posted July 19, 2012 Author Share Posted July 19, 2012 Thank-you so much! So useful. Very easy solution, too! Link to comment Share on other sites More sharing options...
mike77 Posted October 29, 2012 Share Posted October 29, 2012 Finally I can start doing something with PW and learn it from the scratch. I also have a (very) basic question. I couldn't find it in the API and on the forum but I need it straight just to get along with the work. The case is very simple. I added a image field called 'image' and put it into the template. Then, in template code, I added this (just like in case of other fields, textArea for example): <?php echo $page->image;?> but it doesn't work - the photo won't show only the name of the file. I also tried: <img src="<?=$page->image?>" alt="" /> with now result. What is the syntax for adding a single image field? I used the foreach loop with the $page->image and it worked find. Link to comment Share on other sites More sharing options...
Soma Posted October 29, 2012 Share Posted October 29, 2012 If it's a single image field you do simply: echo $page->image->url; If the field allows for multiple you have to loop the field or directly access one through index. $page->image->first()->url; You find some API infos here http://processwire.com/api/cheatsheet/#files And also in the Docs http://processwire.com/api/fieldtypes/images/ Link to comment Share on other sites More sharing options...
mike77 Posted October 29, 2012 Share Posted October 29, 2012 If it's a single image field you do simply: echo $page->image->url; Well, I have tried this previously but the output I get is just an URL like: /../site/assets/files/1016/gk.jpg - instead of photo I see that URL. The folder assets/file/1016 contains the file gk.jpg so it should show but it doesn't. I also checked all image field setting but found nothing that could interact that issue. Could it be that I don't have something installed correctly? Link to comment Share on other sites More sharing options...
Soma Posted October 29, 2012 Share Posted October 29, 2012 Well you'd have to insert it into a image tag to see an image of course. echo $page->image->url only returns the url and not an image tag. You have to create your markup, PW doesn't generally generate markup. So that might look like this. Depends on how you need the image be rendered. $imgurl = $page->image->url; echo "<img src='$imgurl'/>"; That's all also shown in the images documentation with examples: Link to comment Share on other sites More sharing options...
mike77 Posted October 29, 2012 Share Posted October 29, 2012 Thanks Soma, I should try to insert it into markup the way you did instead of taking a shortcut like: echo "<img src='<?=$page->image?>'/>" Anyway the PW variable should always be as a value of a standard variable (like $imgurl in that case) because the code above or echo "<img src='<?php $page->image->url ?>'/>" don't work either. First lesson learnt Link to comment Share on other sites More sharing options...
diogo Posted October 29, 2012 Share Posted October 29, 2012 echo "<img src='<?php $page->image->url ?>'/>" inside phop you can do: echo "<img src='{$page->image->url}'/>"; and outside php: <img src='<?php echo $page->image->url ?>'/> 1 Link to comment Share on other sites More sharing options...
mike77 Posted October 29, 2012 Share Posted October 29, 2012 Additionally I could remind that for single image field the "Maximum files allowed" setting (in the "Details" section in admin panel) should be changed from 0 to 1. Otherwise the PW will seek for an array and not exact file (the $page->image->url output will be a folder not a file name) and you won't see the photo you added through the admin panel. Link to comment Share on other sites More sharing options...
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