RichyRich Posted August 28, 2017 Share Posted August 28, 2017 I have looked into this for some hours, and done plenty searching. I have had something similar executed successfully. But this code returns " Error: Call to a member function size() on a non-object ". If I leave out size() image displays, albeit, too big. Here is code... foreach($pages->find("template=blog-post, limit=12, sort=-blog_date") as $child) { $content .= " <div class=\"col-3\"> <img src=\"{$child->images->first->size(200,200)->url}\"> <a href=\"{$child->url}\">{$child->title} </a> </div>"; } Link to comment Share on other sites More sharing options...
abdus Posted August 28, 2017 Share Posted August 28, 2017 Here's WireArray::first method public function first() { return reset($this->data); } Documentation for reset() function says Returns: the value of the first array element, or false if the array is empty. Error: Call to a member function size() on a non-object i.e. boolean false happens because $child->images is empty for a certain child. A simple JS should help you pinpoint the faulty page. foreach ($pages->find("template=blog-post, limit=12, sort=-blog_date") as $child) { if (!is_object($child->images->first)) { $content .= "<script>alert('something wrong with $child->name')</script>"; } $content .= // ... } 2 Link to comment Share on other sites More sharing options...
kongondo Posted August 28, 2017 Share Posted August 28, 2017 1 hour ago, RichyRich said: <img src=\"{$child->images->first->size(200,200)->url}\"> Unless you changed it, Blog does not have an image field called 'images'. If you didn't change it, then the field is called 'blog_images'. FYI, all fields in Blog are prefixed blog_. The following should get you going... <img src=\"{$child->blog_images->first->size(200,200)->url}\"> 3 Link to comment Share on other sites More sharing options...
RichyRich Posted August 29, 2017 Author Share Posted August 29, 2017 Your posts had lead me to the conclusion! It is simple... I'm an idiot. Processwire was expecting an image on every blog post page. I did not have an image set on everyone one. Now it is... foreach($pages->find("template=blog-post, limit=12, sort=-blog_date") as $child) { if(is_object($child->blog_images->first)) { $featureImage = $child->blog_images->first->size(400,200); } else { $featureImage = NULL;} $content .= " <div class=\"col-3\"> <img src=\"{$featureImage->url}\"> <a href=\"{$child->url}\">{$child->title}</a> </div>"; } with NULL just a placeholder till I find some thing more suitable. I came from MODx and I have to say that the processwire is awesome. I hope to contribute one day. 1 Link to comment Share on other sites More sharing options...
abdus Posted August 29, 2017 Share Posted August 29, 2017 You can do something like this to show a featured image only when child page has one. <?php $posts = $pages->find("template=blog-post, limit=12, sort=-blog_date"); ?> <?php foreach ($posts as $child): ?> <div class="col-3"> <?php if($featured = $child->images->first): ?> <img src="<?= $featured->size(400,200)->url ?>" alt="<?= $featured->description() ?>"> <?php endif; ?> <a href="<?= $child->url ?>"><?= $child->title ?></a> </div> <?php endforeach; ?> I'm not particularly a fan of assignments inside if statement, but it's concise and seems to fit here. 1 Link to comment Share on other sites More sharing options...
szabesz Posted August 29, 2017 Share Posted August 29, 2017 (edited) 6 hours ago, abdus said: I'm not particularly a fan of assignments inside if statement, but it's concise and seems to fit here. Also, one can provide an "image not available" image (an image file with baked-in text) I prefer this approach because I find that most site editors are urged to fix the issue of a placeholder image showing up, while simply leaving out an image often stays ignored. That is why I use something like this: https://processwire.com/talk/topic/16811-addhookmethod-example-for-pageimages/ Edited August 29, 2017 by szabesz typo 2 Link to comment Share on other sites More sharing options...
abdus Posted August 29, 2017 Share Posted August 29, 2017 @szabesz I was looking for that post, but couldn't find it. I'm using the same hook but without a placeholder image. I hope using placeholder will convince my editors. Thanks for linking it ? 1 Link to comment Share on other sites More sharing options...
szabesz Posted August 29, 2017 Share Posted August 29, 2017 I'm glad I shared something useful I have just extended that tip a little bit with "how to use a placeholder image uploaded in the admin instead of a hard coded one". 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