gregory

If it exists, show the div with the image

Recommended Posts

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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 ...

Share this post


Link to post
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 ?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Thanks so much! Now it works perfectly. Can I offer you a coffee? ☕

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

To be exact, I did this: 

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

Thank you again!

  • Like 1

Share this post


Link to post
Share on other sites
11 minutes ago, gregory said:

Can I offer you a coffee? ☕

Already glad you switched from WP to PW  🙊🙈🙉

  • Like 2

Share this post


Link to post
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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.