Hurme Posted March 14, 2018 Share Posted March 14, 2018 Hi, I'm exploring exciting new things (for me) like functions and being the newbie that I am, I'm running into some problems with images. I've gotten far enough that I know I need to add: $pages = wire('pages'); To use $pages inside a function. Now I'm wondering if something similar needs to be done to make $image->width work as well. I have foreach that loops through some pages as $child. This works fine as does everything else except for the images. The important part is. $image = $child->images->first; $out .= '<img src="'.$image->url->width(500).'">'; What I get is: "Fatal error: Call to a member function width() on boolean" + the line number + file name and all the other usual jazz. If I remove the width(500) it works fine. It also works with the width(500) if I use it outside of the function. What to do 'o wise ones? Link to comment Share on other sites More sharing options...
DaveP Posted March 14, 2018 Share Posted March 14, 2018 (edited) Just a guess, but try $out .= '<img src="'.$image->width(500)->url.'">'; because I think your version is trying to change the width of the url. Edited March 14, 2018 by DaveP Link to comment Share on other sites More sharing options...
Hurme Posted March 14, 2018 Author Share Posted March 14, 2018 Hi DaveP, I also tried this, but got the same result. Any time I used width php runs into a wall. Link to comment Share on other sites More sharing options...
ottogal Posted March 14, 2018 Share Posted March 14, 2018 I'd try this: $out .= "<img src='$image->width(500)->url' />"; Link to comment Share on other sites More sharing options...
DaveP Posted March 14, 2018 Share Posted March 14, 2018 Ok, this should work $image = $child->images->first->width(500); $out .= '<img src="'.$image->url.'">'; See http://cheatsheet.processwire.com/files/image-properties-in-addition-to-those-in-file/image-width/ Link to comment Share on other sites More sharing options...
Macrura Posted March 14, 2018 Share Posted March 14, 2018 not sure if it is still the case but used to have to use first as a function, like $image = $child->images->first(); $out .= "<img src='{$image->width(500)}'>"; Link to comment Share on other sites More sharing options...
rafaoski Posted March 14, 2018 Share Posted March 14, 2018 Maybe it will help you _func.php // IMAGE FROM FIRST CHILDREN function myThumb($page) { $out = ''; if ( !count($page->child->images) ) return 'Add Image'; $image = $page->child->images->first; $out .= '<img src="'.$image->width(200)->url.'">'; return $out; } // IMAGES FROM PAGE CHILDRENS function myThumbs($p_children) { $out = ''; foreach ($p_children as $page) { if (count($page->images) ) { $image = $page->images->first; $out .= "<img src='{$page->images->first->width(200)->url}'>"; } } return $out; } basic-page.php <?php echo myThumb($page); ?> <hr> <?php echo myThumbs($page->children); ?> Link to comment Share on other sites More sharing options...
ottogal Posted March 14, 2018 Share Posted March 14, 2018 46 minutes ago, Macrura said: not sure if it is still the case but used to have to use first as a function, like $image = $child->images->first(); $out .= "<img src='{$image->width(500)}'>"; From the API Reference page for WireArray class: Quote first() ......... Can also be used as property: first 1 Link to comment Share on other sites More sharing options...
Hurme Posted March 15, 2018 Author Share Posted March 15, 2018 Hi guys, I didn't try rafaoski's code yet, but I did try everything else you suggested. It all runs into the same error. Below is a stripped down version of the function I'm using for claritys sake. function test($selectTemplate) { $pages = wire('pages'); $children = $pages->find("parent.template={$selectTemplate}, include=all, limit=3"); $out = ''; foreach($children as $child) { $image = $child->images->first; $out .= '<img src="'.$image->width(50)->url.'">'; } return $out; } It works if I remove the "width(50)" part. I've also tried putting the width in different places, as suggested. Link to comment Share on other sites More sharing options...
tpr Posted March 15, 2018 Share Posted March 15, 2018 Does every child page contain an image in the images field? You should check first if there's any. Link to comment Share on other sites More sharing options...
Hurme Posted March 15, 2018 Author Share Posted March 15, 2018 In the real deal it does get tested, in this stripped down version I'm just trying to find out how to get the width to work. Link to comment Share on other sites More sharing options...
tpr Posted March 15, 2018 Share Posted March 15, 2018 Then all child page is of the same template? If not, perhaps the images field is not available or perhaps a template override for the number of images is set to single image. Try to dump the images field for each children in the loop (eg with Tracy Debugger). Link to comment Share on other sites More sharing options...
rafaoski Posted March 15, 2018 Share Posted March 15, 2018 I tested your version on the latest issue of Processwire 3.0.95 with a defolt Profile (site-beginner) and it works if I add into $pages->find() selector => images.count>0 Now he should search only if the field has images images.count>0 _func.php function test($selectTemplate) { $pages = wire('pages'); $children = $pages->find("parent.template={$selectTemplate}, include=all, limit=3, images.count>0"); $out = ''; foreach($children as $child) { $image = $child->images->first; $out .= '<img src="'.$image->width(50)->url.'">'; } return $out; } basic-page.php <?=test('basic-page'); ?> Link to comment Share on other sites More sharing options...
Hurme Posted March 15, 2018 Author Share Posted March 15, 2018 Thanks rafaoski, Maybe it's down to the PW version then. Mine is 3.0.88, I'll update it when I have a moment and report what happens. Link to comment Share on other sites More sharing options...
Hurme Posted March 15, 2018 Author Share Posted March 15, 2018 Everything is working again. Nothing to do with PW version. It was all down to the children with no images after all. After doing better job with checking if the images exist it started working. if (count($child->images)) { // Original code here } Interesting that the width() crashed PHP but without it everything worked fine. Anyway, I'm little bit wiser now. Thank you for your help everyone, and sorry if you had a spend a lot of time mulling over this. 2 Link to comment Share on other sites More sharing options...
tpr Posted March 15, 2018 Share Posted March 15, 2018 Glad you sorted this out. Even though I would change the main selector as @rafaoski suggested, to retrieve only those pages that have images, imo it's a bit more performant. 2 Link to comment Share on other sites More sharing options...
Hurme Posted March 15, 2018 Author Share Posted March 15, 2018 I would, but this is used mostly for news feed with text and other stuff too so I need to get more than the images. 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