Jump to content

PHP Warning: Images and count()


suntrop
 Share

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).

Link to comment
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
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...