I am writing a web and i got stuck. PHP is not my language of choice, so I'm not that used to it. But lets look at the problem:
$i = 1;
$n = 0;
<div class="hs_client_logo_slider">
<?php
foreach ($hashone_client_logo_image as $hashone_client_logo_image_single) {
foreach ($hashone_logo_list as $hashone_logo_url) {
if ($i > $n) {
?>
<a target="_blank" href="<?php echo esc_url($hashone_logo_url) ?>">
<img alt="<?php _e('logo','hashone') ?>" src="<?php echo esc_url(wp_get_attachment_url($hashone_client_logo_image_single)); ?>">
</a>
<?php
$n = $n + 1;
continue 2;
} else {
$i = $i + 1;
continue 1;
Basically I am trying to do:
Foreach (x as y) && Foreach (a as b) {
/* Magic happens here */
In the first foreach I have images and in the second one links. And for the first image I need the first link stored in logo_list, for the second image I need second link and so on... So I tried If statement that could take care of it, but for some reason it doesn't work.
Now it gives me first image with first link, but every image after that is stuck with second link. Do you have any idea what to do?
I'm glad for everyone who tries to help me out. Thank you very much.
Try a different approach. First of all, prepare your data before passing it to foreach loop that is responsible for generating view, then your foreach loop will be much easier to read and maintain.
If every 1st element should go with 1st element in the second foreach, 2nd to 2nd, 3rd to 3rd and so on, then using two foreach loops is redundant. Use only 1 foreach and in every foreach iteration take its index (it can be a separate incremented variable) and take element from second array by index. You can also use a standard for loop and it should be even simpler to implement.