Jump to content

Recommended Posts

Posted

Hi guys, 

how can I do with php to show the div containing the image (if the image exists)?
Thanks for your help.

image.jpg

Posted

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
Posted

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

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

Posted

It is right (sorry I come from WP). But in my case I have a page with a list of articles and the image field is also called in the article that has no image.

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

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