How do I test for images?
Started by everfreecreative, Apr 20 2012 10:27 AM
6 replies to this topic
#1
Posted 20 April 2012 - 10:27 AM
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!
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!
#2
Posted 20 April 2012 - 11:03 AM
<?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 } ?>
#5
Posted 23 April 2012 - 08:45 AM
you can also use
if($page->images->first()) ...
@somartist | modules created | support me, flattr my work flattr.com
#6
Posted 23 April 2012 - 12:12 PM
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:
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:
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:
...or pages that have one or more 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");
#7
Posted 23 April 2012 - 01:00 PM
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!
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users













