kaz Posted March 27 Share Posted March 27 Funny, how an if statement behaves. I wanna check whether a field has content. So I asked about the field: if ($article->gallery_images) If yes, I only want to output only the first image as a preview in an summary page, that's why I only retrieve these first image: if ($article->gallery_images) { echo " <div class='' uk-grid> <div class='uk-width-1-5@m uk-width-1-1@s'> <img src='{$article->gallery_images->first->url}' alt='{$article->gallery_images->first->description}'> </div> <div class='uk-width-4-5@m uk-width-1-1@s'> $content </div> </div>"; } else { <div class='uk-width-1-1'> $content </div>"; } The query does not work, even if no data is uploaded to the gallery_images field, is else not displayed. Now the funny part: When I add ->first to the if query, else is displayed, the query will work: if ($article->gallery_images->first) { echo " <div class='' uk-grid> <div class='uk-width-1-5@m uk-width-1-1@s'> <img src='{$article->gallery_images->first->url}' alt='{$article->gallery_images->first->description}'> </div> <div class='uk-width-4-5@m uk-width-1-1@s'> $content </div> </div>"; } else { <div class='uk-width-1-1'> $content </div>"; } I have hardly any PHP experience, so perhaps I don't understand why this is the case, but: it surprised me in terms of logic. Link to comment Share on other sites More sharing options...
zoeck Posted March 27 Share Posted March 27 "if ($article->gallery_images)" just works if the field is set to one image only. You have to use "if (count($article->gallery_images))" if you want to check whether one or more images are available. https://processwire.com/docs/fields/images/ -> "How to tell if a page has images present" 4 Link to comment Share on other sites More sharing options...
kaz Posted March 28 Author Share Posted March 28 @zoeck interesting, I didn't know that in an array the search is different 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