Suddenly starting getting this error for the following code:
<?php foreach (get_the_terms(get_the_ID(), 'loan-club') as $cat) : ?>
<img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" title="ON LOAN AT: <?php echo $cat->name; ?>" />
<?php endforeach; ?>
Sometimes the taxonomy 'loan-club' is empty. Could that be the problem? If so, could someone point me towards the correct code?
Add if condition before foreach:
<?php
$loan_club = get_the_terms(get_the_ID(), 'loan-club');
if(is_array($loan_club)) {
foreach ($loan_club as $cat) {
?>
<img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" title="ON LOAN AT: <?php echo $cat->name; ?>" />
<?php
}
}
?>
Look at get_the_terms() function in the documentation: https://developer.wordpress.org/reference/functions/get_the_terms/
The function may also return WP_Error or false. If WP_Error or false is returned this will cause the error and the foreach loop breaks.
<?php foreach ((array)get_the_terms(get_the_ID(), 'loan-club') as $cat) : ?>
<img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" title="ON LOAN AT: <?php echo $cat->name; ?>" />
<?php endforeach; ?>
Note: to all the people complaining about typecast, please note that the OP asked cleanest way to skip a foreach if array is empty (emphasis is mine). A value of true, false, numbers or strings is not considered empty.
Related
When I echo my own variable to a div container it comes out as plaint text (unformatted). I don't understand why echoing a Magento variable comes out formatted but mine doesn't? Here's my code, in particular the <?php if(!isset($specialPrice)): { ?> section which I created. Here is the code:
<div class="product">
<a href="<?php echo $_item->getProductUrl() ?>"
title="<?php echo $this->escapeHtml($_item->getName()) ?>" class="product-image"><img
src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail') ?>"
alt="<?php echo $this->escapeHtml($_item->getName()) ?>"/></a>
<div class="product-details">
<p class="product-name">
<?php echo $this->escapeHtml($_item->getName()) ?>
</p>
<?php $specialPrice = $productToCheck->getData('special_price');
$orignalPrice = $productToCheck->getData('price');
?>
<?php if(!isset($specialPrice)): { ?>
<?php echo $product['price'] ?>
<?php } else: { ?>
<?php echo $specialPrice ?>
<?php } endif ?>
</div>
</div>
Echoing $product['price'] shows up with its CSS like this:
but if it enters the ELSE statement to display my variable it shows like this:
Does anyone know what could be going wrong?
$product['price'] returns you a unformatting price value.
Maybe you can call a block function, for example $block->getPrice() and in getPrice() you need to format the price by your custom preferences.
If you want to add styles to your price value then you need to use a magento tag class
Apologies if this has already been asked (I cannot find an answer) but I am using PHP and I am building a slider, but would like two images per slide, not one. So, in theory the foreach() needs to include two per each.
An example of the setup is as follows:
<?php foreach ($page->images as $image) : ?>
<img src="<?php echo $image->url; ?>"/>
<?php endforeach; ?>
I was thinking I could do something like a count...
<?php $index = 0; foreach ($page->images as $image) : ?>
<img src="<?php echo $image->url; ?>"/>
<?php $index++; ?>
<?php if ( $index % 2 == 0 && $index !=count($page->images) ) : ?>
<li></li>
<?php endif; ?>
<?php endforeach; ?>
But I got a little confused as this would insert something every 2... not include two of whatever the foreach loop is fetching at once.
Hope this makes sense and thanks in advance
Why so complicated? Use a simple for loop instead:
<?php for ($i=0; $i<count($page->images)-1; $i+=2) { ?>
<img src="<?php echo $page->images[$i]->url; ?>"/>
<img src="<?php echo $page->images[$i+1]->url; ?>"/>
<?php } ?>
Or even more elegant, a do/while loop:
<?php $i=0; do { ?>
<img src="<?php echo $page->images[$i++]->url; ?>"/>
<img src="<?php echo $page->images[$i++]->url; ?>"/>
<?php } while ($i<count($page->images)) ?>
Compared to using a foreach loop these approaches have another advantage: you do not create copies of all objects. This can make a huge difference if those objects are non-trivial.
Okay, I managed to work this out... hopefully it will help.
<div class="each-slide">
<?php $index = 0; foreach ($page->images as $image) : ?>
<img src="<?php echo $image->url; ?>"/>
<?php $index++; ?>
<?php if ( $index % 2 == 0 && $index !=count($page->images) ) : ?>
</div><div class="each-slide">
<?php endif; ?>
<?php endforeach; ?>
</div>
I am looking to offset a group of PHP array results to create multiple rows of data.
For example, the first row will contain the first four array results, with the second row displaying results 5-8 and the third row containing results 9-12.
Currently, I am printing out all 12 in one list, however I'd like a bit more display control (i.e. ordering the results into columns which I can style independent of each), hence why I'd like to offset the results.
My current PHP is below:
<?php
if (empty($tmdb_cast['cast'])) {
} else {?>
<section class="cast">
<p class="title">Cast</p>
<ul class="section_body"><?php
foreach($tmdb_cast['cast'] as $key => $castMember){
if ($key < 12) {?>
<li class="actor_instance clearfix"><?php
if ($castMember['profile_path'] != '') {?>
<img src="http://cf2.imgobject.com/t/p/w45<?php echo $castMember['profile_path']; ?>" class="actor_image pull-left" alt="<?php echo $castMember['name']; ?>" title="<?php echo $castMember['name']; ?>" /><?php
} else {?>
<img src="assets/images/castpic_unavailable.png" class="actor_image pull-left" alt="No image available" title="No image available" /><?php
}?>
<p class="actor"><?php echo $castMember['character']; ?> - <?php echo $castMember['name']; ?></p>
</li><?php
}
}?>
<div class="morecast pull-right">[...view all cast]</div>
</ul>
</section><?php
}?>
P.S. Apologies for how I've formatted the code in the above block - I'm still not 100% sure how to make it look "nice" on StackOverflow.
Use array_chunk to break a single dimension array into a 2D array. Then you can loop through each chunk, and then each result, to get that "offset" effect between them.
$chunks = array_chunk($tmdb_cast['cast'], 4); // 4 here, is the number you want each group to have
foreach($chunks as $key => $chunk)
{
foreach($chunk as $castMember)
{
//display castMember here
}
// add code between groups of 4 cast members here
}
First: mixing php and html this way is a very bad habbit... one day you will have to maintain your code and that day you will vomit all over your desk because you mixed different languages in one file...
That being said..
and without creating a new array:
foreach($tmdb_cast['cast'] as $key => $castMember)
{
if ($key < 12)
{
// your code goes here
// if key is 3, or 7 the code below will close the listcontainer and open a new one...
if( ($key+1)%4 == 0 AND $key <11)
echo '</ul> <ul class="section_body">';
}
}
Also replace this:
if (empty($tmdb_cast['cast']))
{
}
else {
with this:
if (!empty($tmdb_cast['cast']))
{
I'm not entirely sure what you're looking for but here's how I would format your code. Alternative syntax for 'if' is a little more readable, and the use of for loops instead of a foreach will give you the control over row numbers you say you're looking for.
<?php if( ! empty($tmdb_cast['cast'])): ?>
<section class="cast">
<p class="title">Cast</p>
<ul class="section_body">
<?php
for($i = 0; $i < 12; $i++):
$castMember = $tmdb_cast['cast'][$i];
?>
<li class="actor_instance clearfix">
<?php if($castMember['profile_path'] != ''): ?>
<img src="http://cf2.imgobject.com/t/p/w45<?php echo $castMember['profile_path']; ?>" class="actor_image pull-left" alt="<?php echo $castMember['name']; ?>" title="<?php echo $castMember['name']; ?>" />
<?php else: ?>
<img src="assets/images/castpic_unavailable.png" class="actor_image pull-left" alt="No image available" title="No image available" />
<?php endif; ?>
<p class="actor"><?php echo $castMember['character']; ?> - <?php echo $castMember['name']; ?></p>
</li>
<?php endfor; ?>
<div class="morecast pull-right">[...view all cast]</div>
</ul>
</section>
<?php endif; ?>
here is my code:
<?php
foreach ($productsRecord['images'] as $upload):?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['urlPath'] ?>" alt="" /><br />
<?php endif ?>
<?php endforeach ?>
How would I go about limiting the results to only the first result, would I use a break; statement?
Cheers
Try to use current()
your code shall like that:
<?php reset($productsRecord['images']); ?>
<?php $upload = current($productsRecord['images']);?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['urlPath'] ?>" alt="" /><br />
<?php endif ?>
To more information about the current() check the manual:
http://php.net/manual/en/function.current.php
Just use the first element of the array directly
$productsRecord['images'][0]
No need to loop here.
Yes, use break
<?php break; endforeach; ?>
Overview
I am trying to get a photo feed on to my site using Flickr's api and the phpflickr library. I can successfully get the photoset on to my site, but it shows all the photos from every photoset, what I was hoping to achieve was to show the primary photo from each photoset, and then if the user clicked on the image it would show the full photoset in a lightbox/shadowbox.
My Code
<div id="images" class="tabnav">
<ul class="items">
<?php $count = 1; ?>
<?php foreach ($photosets['photoset'] as $ph_set): ?>
<?php $parentID = $ph_set['parent']; ?>
<?php $photoset_id = $ph_set['id'];
$photos = $f->photosets_getPhotos($photoset_id);
foreach ($photos['photoset']['photo'] as $photo): ?>
<li>
<a rel="shadowbox['<?=$count;?>']" href="<?= $f->buildPhotoURL($photo, 'medium') ?>" title="<?= $photo['title'] ?>">
<img src="<?= $f->buildPhotoURL($photo, 'rectangle') ?>" alt="<?= $photo['title'] ?>" width="210" height="160" title="<?= $photo['title'] ?>" />
<h3><?=$ph_set['title']?></h3>
<p><?=$ph_set['description'];?></p>
</a>
</li>
<?php endforeach; ?>
<?php $count++; ?>
<?php endforeach; ?>
</ul>
</div>
Another Attempt
I have also tried calling the getPhotos function differently, instead of sending it without any parameters I sent it with parameters
$photos = $f->photosets_getPhotos($photoset_id, NULL, NULL, 1, NULL);
The above code stopped the showing all the photos from each photoset and started showing just the primary image, but it also stopped making the rest of the photos accesible to me.
Is there something I can do to make this work? I am totally out iof ideas.
Regards and thanks
I came up with this soltion, thought I would post it in case anyone else hits this problem,
<?php $count = 1; ?>
<?php foreach ($photosets['photoset'] as $ph_set): ?>
<?php $parentID = $ph_set['parent']; ?>
<li>
<?php $photoset_id = $ph_set['id'];
$photos = $f->photosets_getPhotos($photoset_id);
foreach ($photos['photoset']['photo'] as $photo): ?>
<?php if($parentID == $ph_set['parent']): ?>
<a rel="lightbox[album<?=$count;?>]" href="<?= $f->buildPhotoURL($photo, 'medium') ?>" title="<?= $photo['title'] ?>">
<?php endif;?>
<img src="<?= $f->buildPhotoURL($photo, 'rectangle') ?>" alt="<?= $photo['title'] ?>" width="210" height="160" title="<?= $photo['title'] ?>" />
<h3><?=$ph_set['title']?></h3>
<?php if($ph_set['description'] != null) :?>
<p><?=$ph_set['description'];?></p>
<?php endif; ?>
<?php if($parentID == $ph_set['parent']): ?>
</a>
<?php endif;?>
<?php endforeach; ?>
</li>
<?php $count++; ?>
What you'll probably want to do is starting by iterating through the whole array and grouping each album into a separate array first and making a special array for your album's main photo.
Then you can easily iterate through the arrays to display each album and the code becomes much more maintainable.