Jump to content
suntrop

PHP Warning: Images and count()

Recommended Posts

When I check if the page has an image attached, I get the error 

PHP Warning: count(): Parameter must be an array or an object that implements Countable in 

But this should be the correct way

if ( count(page()->images) ) {
   $heroimage = page()->images->first()->url;
} elseif ( pages()->get('/')->images->first()->url ) {
   $heroimage = pages()->get('/')->images->first()->url;
}

I found some issues with this on GitHub, but it was fixed. Using PHP 7.3 and PW 148 (same issue before update).

Share this post


Link to post
Share on other sites

This works for me here:

if ( count(page()->images) ) {
    echo "yay!";
}

ProcessWire: 3.0.148
PHP: 7.3.13
Webserver: Apache/2.4.35 (Win64) OpenSSL/1.1.1b
MySQL: 5.7.24

but you don't even need count... this works as well:

if ( page()->images) {
    echo "yay!";
}

and if you really need the number of images, this works too (always has):

$imageCount = page()->images->count()

 

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...