Jump to content

Recommended Posts

Posted

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!

Posted
<?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
Posted

Everfree, there seem to be many different responses to this. It would be great if all the possibilities were listed in one reference page.

Perhaps with the method and the types of field to which it applies.

Posted

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
Posted

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!

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
  • Recently Browsing   0 members

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