Jump to content

If it exists, show the div with the image


gregory
 Share

Recommended Posts

Hi,

Quote

How to tell if a page has images present

It is a good idea to check if an image(s) field has something in it before attempting to output it. When set to contain multiple images, you can tell if a page contains one or more images by using the count() function:


if(count($page->images)) {
  // the page has one or more images
}   

When set to contain a max of one (1) image, then you can just check if your image field has a value:


if($page->image) {
  // an image is present
} 

 

more infos: https://processwire.com/api/fieldtypes/images/

  • Like 2
Link to comment
Share on other sites

Thank You for your response. 

In my case I need to write this, because I am in the post archive:

if ($ story-> imagefieldname) {
...
}

But it does not work as it should because it recalls an image in other posts ...

Link to comment
Share on other sites

7 minutes ago, gregory said:

In my case I need to write this, because I am in the post archive:

Sorry, I don't understand what you mean by 'post archive'.

The if condition you write, only work if the imagefieldname field is configured to contain a max of one (1) image. It is the case ?

Link to comment
Share on other sites

1 hour ago, gregory said:

show the div containing the image (if the image exists)

I think I am missing something, because to achieve what you asked, you have to write :

[...]

foreach($stories as $story) {
    if(count($page->imagefieldname)) { // images exist, we show the div
	echo "<div>...</div>";
    }
    
    [...]
}

[...]

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

6 minutes ago, flydev said:

Already glad you switched from WP to PW  ???

Now the new websites I build them only with Processwire. It is a powerful CMS and the community is very serious and professional. ?

  • Like 2
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...