Jon E Posted September 24, 2012 Posted September 24, 2012 Hi, Sorry this is probably very basic logic, but I wondered what you would suggest if my code is: <?php $features = $pages->find("template=portfolio, limit=4, sort=-date"); foreach($features as $feature) { $thumb = $feature->thumbnail->size(244,244); echo "<li>" . "<a href='{$feature->url}'><div id='explore_thumb'><img src='{$thumb->url}'></div>" . "<h3>{$feature->title}</h3>" . "<h4>{$feature->summary}</h4></a>" . "</li>"; } ?> And I want to output a placeholder image in the instance that a thumbnail isn't uploaded... Thanks, Jon
diogo Posted September 24, 2012 Posted September 24, 2012 <?php $features = $pages->find("template=portfolio, limit=4, sort=-date"); foreach($features as $feature) { $thumb = $feature->thumbnail ? $feature->thumbnail->size(244,244)->url : "[url to the placeholder]"; // if no thumbnail, replace by placeholder echo "<li>" . "<a href='{$feature->url}'><div id='explore_thumb'><img src='{$thumb}'></div>" . //changed from $thumb->url to only $thumb "<h3>{$feature->title}</h3>" . "<h4>{$feature->summary}</h4></a>" . "</li>"; } ?> 2
SiNNuT Posted September 24, 2012 Posted September 24, 2012 It's pretty obvious because of diogo's code comment but just in case you're wondering; he's using a so called ternary operator. Which is basically the same as this If/Else statement: if($feature->thumbnail) { $thumb = $feature->thumbnail->size(244,244)->url; }else{ $thumb = "[url to the placeholder]"; } 2
diogo Posted September 24, 2012 Posted September 24, 2012 Thanks SiNNuT, I was just about to edit my post to refer that.
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