Jump to content

image resize returning: function size() on a non-object


RichyRich
 Share

Recommended Posts

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

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 .= // ...
}
  • Like 2
Link to comment
Share on other sites

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}\">

 

  • Like 3
Link to comment
Share on other sites

Your posts had lead me to the conclusion! It is simple... I'm an idiot. :lol:

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.

  • Like 1
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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 by szabesz
typo
  • Like 2
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...