Jump to content

How do I test for images?


thetuningspoon
 Share

Recommended Posts

Hey guys,

I think I'm really starting to get the hang of working with the ProcessWire api, following along with the cheatsheet. However, I'm drawing a blank on this.

How do I test to see if the images field of a specific page contains any images? Same goes for the other field types. I know you can test for the existence of the field type on the page, but can you test for whether it has any data in it?

Thanks!

Link to comment
Share on other sites

<?php if ($page->photo->eq(0)) { ?>
<img src="<?php echo $page->photo->eq(0)->url; ?>" alt="<?php echo $page->photo->eq(0)->description; ?>">
<?php } else { ?>
<img src="<?php echo $config->urls->templates; ?>pics/nophoto.png" alt="Sorry, no picture available">
<?php } ?>
  • Like 1
Link to comment
Share on other sites

For a field that can contain multiple images, the value of that field is always going to be a type of array. So you'd have to either check to see if there are any items in the array, or try to retrieve the first time like Soma did. Here's how I usually check if there are any images:

if(count($page->images)) {
   // images here
} else {
   // no images
}

When it comes to a single image field, you are dealing with just one item that is either set or it isn't (rather than an array). You only need to check if it has a value:

if($page->image) {
   // image here
} else {
   // no image
}

When you use $pages->find(), $pages->get(), $page->children(), etc., you can also find pages that have a given amount of images. So if you wanted to find pages that don't have any images, you could do this:

$pages->find("images.count=0"); 

...or pages that have one or more images:

$pages->find("images.count>0"); 
  • Like 6
  • Thanks 1
Link to comment
Share on other sites

For a field that can contain multiple images, the value of that field is always going to be a type of array. So you'd have to either check to see if there are any items in the array, or try to retrieve the first time like Soma did. Here's how I usually check if there are any images:

if(count($page->images)) {
// images here
} else {
// no images
}

When it comes to a single image field, you are dealing with just one item that is either set or it isn't (rather than an array). You only need to check if it has a value:

if($page->image) {
// image here
} else {
// no image
}

When you use $pages->find(), $pages->get(), $page->children(), etc., you can also find pages that have a given amount of images. So if you wanted to find pages that don't have any images, you could do this:

$pages->find("images.count=0"); 

...or pages that have one or more images:

$pages->find("images.count>0"); 

Thanks Ryan that clears it up for me!

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