Search the Community
Showing results for tags 'if else'.
-
I have the following code which creates a 2 column div. In the first I check to see if there are any images present and only echo an image if there is. If I don't do this, I get an error. But I was wondering if there was a more efficient way to do this? I don't know the proper terminology but I suspect breaking the code into three parts as I have done ... First - the first <div class='uk-width-1-2@s'> Second - the image check Third - the final div ...is somewhat more verbose than it needs to be with more modern PHP <?php $imgoptions = array('quality' => 100,'upscaling' => false); $products = $page->siblings("id!=$page"); foreach ($products as $prod){ echo" <div class='uk-width-1-2@s'> "; if(count($prod->images)){ echo" <a href='$prod->url'> <img class='lazyload prod-thumb-ov' src='{$prod->images->first()->height(300, $imgoptions)->url}' data-src='{$prod->images->first()->height(300, $imgoptions)->url}' data-srcset=' {$prod->images->first()->width(414)->url} 414w, {$prod->images->first()->width(320)->url} 320w' data-sizes='auto' alt='{$prod->images->first()->description}'> </a> ";} echo " </div> <div class='uk-width-1-2@s'> {$prod->title} </div> ";} ?> Thanks
-
I'm trying a twist on a simple if / else statement. If the blog_category field is populated (page select), display the categories. If not, don't output anything. This works: <?php if ($page->blog_category){ foreach($page->blog_category as $cat){ echo"<a href=\"{$cat->url}\">{$cat->title}</a></li> ";} else { echo"";} ?> But I've an added complication that the Categories need to have a "Posted under" before the very first Category. I've tried variations on the code below and the first part works but even if there are no categories selected, I still get "posted under" appearing on the page. <?php if ($page->blog_category){ echo"Posted under: "; foreach($page->blog_category as $cat){ echo"<a href=\"{$cat->url}\">{$cat->title}</a></li> ";} } else { foreach($page->blog_category as $cat){ echo"";} } ?> Have run my PHP through an online syntax checker and it's not showing any errors so it must be an issue. Any tips?