gregory Posted May 16, 2018 Share Posted May 16, 2018 Hi guys, how can I do with php to show the div containing the image (if the image exists)? Thanks for your help. Link to comment Share on other sites More sharing options...
flydev Posted May 16, 2018 Share Posted May 16, 2018 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/ 2 Link to comment Share on other sites More sharing options...
gregory Posted May 16, 2018 Author Share Posted May 16, 2018 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 More sharing options...
flydev Posted May 16, 2018 Share Posted May 16, 2018 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 More sharing options...
gregory Posted May 16, 2018 Author Share Posted May 16, 2018 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. Link to comment Share on other sites More sharing options...
flydev Posted May 16, 2018 Share Posted May 16, 2018 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>"; } [...] } [...] 1 1 Link to comment Share on other sites More sharing options...
gregory Posted May 16, 2018 Author Share Posted May 16, 2018 Thanks so much! Now it works perfectly. Can I offer you a coffee? ☕ 1 1 Link to comment Share on other sites More sharing options...
gregory Posted May 16, 2018 Author Share Posted May 16, 2018 To be exact, I did this: if(count($story->imagefieldname)) { ... } Thank you again! 1 Link to comment Share on other sites More sharing options...
flydev Posted May 16, 2018 Share Posted May 16, 2018 11 minutes ago, gregory said: Can I offer you a coffee? ☕ Already glad you switched from WP to PW ??? 2 Link to comment Share on other sites More sharing options...
gregory Posted May 16, 2018 Author Share Posted May 16, 2018 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. ? 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now