Passing multiple arrays to for each loop..i think - php

I love jQuery and dont really understand php.
I am making a slider with albums. So far so good except I am now stuck trying to add two arrays into one for each loop. At least I think that is the best solution.
In my code you can see I have achieved what I need by hard coding 6 images for the 6 custom fields in the custom post type (I am using wordpress). The problem is that if there are not six images then the slider show a blank image (as it exists but doesnt have a src). I tried removing the element with jquery but that was no good. Here is the code I have so far, perhaps there is something i am missing, I just cant seem to get the logic quite right.
<?php
$args = array(
'post_type' => 'albums_gallery',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$album_name = get_the_ID();
$image_1 = get_field('image_1');
$image_1_url = $image_1['url'];
$image_1_caption = get_field('image_1_caption');
$image_2 = get_field('image_2');
$image_2_url = $image_2['url'];
$image_2_caption = get_field('image_2_caption');
$image_3 = get_field('image_3');
$image_3_url = $image_3['url'];
$image_3_caption = get_field('image_3_caption');
$image_4 = get_field('image_4');
$image_4_url = $image_4['url'];
$image_4_caption = get_field('image_4_caption');
$image_5 = get_field('image_5');
$image_5_url = $image_5['url'];
$image_5_caption = get_field('image_5_caption');
$image_6 = get_field('image_6');
$image_6_url = $image_6['url'];
$image_6_caption = get_field('image_6_caption');
?>
<div class="album album_<?php echo $album_name ?>">
<div class="slider-wrapper theme-default">
<div class="slider" class="nivoSlider">
<!--<img src="<?php echo $image_1['url']; ?>" alt="<?php echo $image_1['alt']; ?>" title="<?php echo $image_1_caption; ?>" />
<img src="<?php echo $image_2['url']; ?>" alt="<?php echo $image_2['alt']; ?>" title="<?php echo $image_2_caption; ?>" />
<img src="<?php echo $image_3['url']; ?>" alt="<?php echo $image_3['alt']; ?>" title="<?php echo $image_3_caption; ?>" />
<img src="<?php echo $image_4['url']; ?>" alt="<?php echo $image_4['alt']; ?>" title="<?php echo $image_4_caption; ?>" />
<img src="<?php echo $image_5['url']; ?>" alt="<?php echo $image_5['alt']; ?>" title="<?php echo $image_5_caption; ?>" />
<img src="<?php echo $image_6['url']; ?>" alt="<?php echo $image_6['alt']; ?>" title="<?php echo $image_6_caption; ?>" />-->
<?php
$images = array("$image_1_url","$image_2_url","$image_3_url","$image_4_url", "$image_5_url", "$image_6_url");
foreach ($images as $image) {
if ($image != "") {
echo "<img src='";
echo $image;
echo "' ";
echo "title='caption'";
echo "/>";
}
};
?>
</div>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
I need to add the image caption, and probably a link into the for each and if statements, little out of my depth being a designer.
Thanks for any help.

This is all you need
<div class = "slider-wrapper theme-default">
<div class = "slider" class = "nivoSlider">
<?php
$images = Array();
for($i = 1; $i <= 6; $i++) {
$image = get_field("image_{$i}");
if(!$image || !$image['url']) {
break;
}
$caption = get_field("image_{$i}_caption");
?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" title="<?php echo $caption; ?>" />
<?php
}
?>

Related

Wordpress PHP grid layout presentation

I want to change the layout of a grid of videos on a site. It's Wordpress.
Currently the PHP displays the grid in a 3/2 layout over two rows and then duplicates it depending on how many videos you choose to display.
I understand the code enough to know that we are setting 5, and splitting into 3/2. And i can set it to 6 and split into 3/3 or 4/2.....and so on
But I need to change the layout so we have a row of 3, then 3 but with the first two of the second row stacked on top of each other 1/3rd width and the third 2/3rd width. like below.
new layout to be acheived
Current PHP that does the layout: ( I understand that CSS might have to be changed but I can do that)
<article class="col-1-<?php if(($i%5==0)||(($i+1)%5==0)){ echo "2"; } else { echo "3"; } ?>" id="post-<?php echo $page->ID; ?>">
Full block:
<article class="col-1-<?php if(($i%5==0)||(($i+1)%5==0)){ echo "2"; } else { echo "3"; } ?>" id="post-<?php echo $page->ID; ?>">
<a href="<?php echo get_page_link($page->ID); ?>">
<?php if ( $detect->isMobile() ) {
if (has_post_thumbnail( $page->ID ) ){ ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo $title; ?>" width="160" height="90">
<?php } ?>
<?php } else { ?>
<?php if(get_field('looping_thumb_preview', $page->ID)){ ?>
<video playinline preload="auto" loop="yes">
<source src="<?php echo get_field('looping_thumb_preview', $page->ID); ?>" type="video/mp4">
Your browser does not support the video tag.
</video>
<?php } else if(get_field('looping_thumb', $page->ID)){ ?>
<video playinline preload="auto" loop="yes">
<source src="<?php echo get_field('looping_thumb', $page->ID); ?>" type="video/mp4">Your browser does not support the video tag.</video>
<?php } else if (has_post_thumbnail( $page->ID ) ){ ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo $title; ?>">
<?php } else { ?>
<img src="<?php bloginfo( 'template_url' ); ?>/images/blank.png" alt="<?php echo $title; ?>">
<?php } ?>
<?php } ?>
<div class="center-content">
<h1<?php if(get_field('subtitle', $page->ID)){ echo ' class="hassubtitle"';} else { echo ' class="nosubtitle"'; }?>><?php echo $title;?></h1>
<div class="underline"></div>
<?php if(get_field('subtitle', $page->ID)) { echo "<h2>" . get_field('subtitle', $page->ID) . "</h2>"; }
?>
</div>
</a>
</article>
Thanks in advance for any help

How to loop a php code?

I wanted to do the DRY approach in my code but I'm having a hard time figuring it out. And also, I want to hide the entire code if there's no image_1. Hope you could help me do the trick.
Here's the code
<div class="col-md-4">
<?php
$image = get_field('image_1');
if(get_field('image_1'))
{
echo '<a href="' . get_field('image_link_1') . '">';?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
<div class="col-md-4">
<?php
$image = get_field('image_2');
if(get_field('image_2'))
{
echo '<a href="' . get_field('image_link_2') . '">';?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
<div class="col-md-4">
<?php
$image = get_field('image_3');
if(get_field('image_3'))
{
echo '<a href="' . get_field('image_link_3') . '">';?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
You should put differences to arrays and then wrap everything into for loop:
<?php
$images = array('image_1', 'image_2', 'image_3');
$links = array('image_link_1', 'image_link_2', 'image_link_3');
for($i=0; $i<3; $i++){
?>
<div class="col-md-4">
<?php
$image = get_field($images[$i]);
if(get_field($images[$i])){
echo '<a href="' . get_field($links[$i]) . '">';
?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
}
?>
</div>
<?php
}
?>
Just a hint...:
<?php
for ($i = 0; $i < 3; $i++) {
echo "<div class='col-md-4'>" . "\n";
$image = get_field("image_" . ($i + 1));
...
echo "</div>" . "\n";
}
?>
Something along these lines should get you started if I'm understanding you correctly:
<?php for ($q = 1; $q <= 3; $q++) {
$image_loop = 'image_' . $q;
echo '<div class="col-md-4">';
if ($image = get_field($image_loop)) {
echo '<a href="' . get_field('image_link_' . $q) . '">';
?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
<?php } // end loop ?>
The other suggestions here will work as well but here's what I would do:
First arrange the images in an associative array with keys being the image name and values being the image link and then iterate via a foreach loop.
I generally try to not echo HTML unless strictly necessary.
<?php
$array = [
"image_1" => "image_link_1",
"image_2" => "image_link_2",
"image_3" => "image_link_3",
"image_4" => "image_link_4"
];
foreach ($array as $name => $link):
$image = get_field($name);
if ($image): ?>
<div class="col-md-4">
<a href="<?=get_field($link)?>">
<img src="<?= $image['url']; ?>" title="<?= $image['title']; ?>" alt="<?= $image['alt']; ?>" />
</a>
<?php else: ?>
<img src="http://localhost/image.png">
<?php endif; ?>
</div>
<?php endforeach; ?>

Shorten PHP code with While Loop

I'm trying to simplify the code below, but I'm still struggling to understand it. Code A works fine, it outputs a list of images, but Code B just shows a single image
Code A
<?php
$html_form = '<label>
<img src="%1$s" width="%2$s" height="%3$s" alt="%4$s" />
<input type="checkbox" name="%5$s[]" value="%6$s" %7$s/>
</label>';
$html_td = '';
$html = '';
while ($query->have_posts()) {
$query->the_post();
$id = get_the_ID();
$select = '';
$thumb = wp_get_attachment_image_src($id, array(30, 30));
if (is_array($instance['thumbs']) && in_array($id, $instance['thumbs'])) {
$select = 'checked="checked"';
}
$html .= sprintf($html_td);
$html_td = '';
$html_td .= sprintf($html_form, $thumb[0], $thumb[1], $thumb[2], get_the_title(), $this->get_field_name('thumbs'), $id, $select);
}
$html_form = '
<p>
%s
</p>';
printf($html_form, $html);
?>
My Attempt :
Code B
<?php
while ($query->have_posts()) {
$query->the_post();
$id = get_the_ID();
$thumb = wp_get_attachment_image_src($id, array(30, 30));
$thumbs = is_array($instance['thumbs']) ? (bool) in_array($id, $instance['thumbs']) : true;
}
?>
<p>
<label>
<img src="<?php echo $thumb[0] ?>" width="<?php echo $thumb[1] ?>" height="<?php echo $thumb[2] ?>" alt="<?php echo get_the_title() ?>" />
<input class="checkbox" type="checkbox" name="<?php echo $this->get_field_name('thumbs'); ?>" value="<?php echo $id ?>" <?php checked($thumbs); ?>/>
</label>
</p>
As mentioned in the comments it's because you're only echoing out one image.
You could do something like this:
<?php while($query->have_posts()):
$query->the_post();
$id = get_the_id();
$thumb = wp_get_attachment_image_src($id, array(30, 30));
$thumbs = is_array($instance['thumbs']) ? (bool) in_array($id, $instance['thumbs']) : true;?>
<p>
<label>
<img src="<?php echo $thumb[0] ?>" width="<?php echo $thumb[1] ?>" height="<?php echo $thumb[2] ?>" alt="<?php echo get_the_title() ?>" />
<input class="checkbox" type="checkbox" name="<?php echo $this->get_field_name('thumbs'); ?>" value="<?php echo $id ?>" <?php checked($thumbs); ?>/>
</label>
</p>
<?php endwhile;
wp_reset_postdata();
In this case the HTML is inside the while and an image will echo out for each iteration. Also, make sure you have the wp_reset_postdata() or else you may (or may not) encounter bugs in the future.

Output reverse count foreach loop php

I have the follow foreach loop:
<?php $arrayforward = get_field('background_slider');
$arrayreversed = array_reverse($arrayforward);
$count=1;
foreach($arrayreversed as $subarray) {
$subfield1 = $subarray['background_image'];
$subfield2 = $subarray['background_image_alt'];
$subfield3 = $subarray['text_image'];
$subfield4 = $subarray['text_image_alt']; ?>
<div id="slide<?php echo $count; ?>" class="contentslider_wrapper">
<div class="fullpageimage">
<img id="slideImg<?php echo $count; ?>" src="<?php echo $subfield1; ?>" alt="<?php the_sub_field('background_image_alt'); ?>" />
<img id="textImg<?php echo $count; ?>" class="slidetext" src="<?php echo $subfield2; ?>" alt="<?php the_sub_field('text_image_alt'); ?>" />
</div>
</div>
<?php $count++; }; ?>
I need help getting the count to reverse. The output I am looking for is as follows:
<div id="slide4"></div>
<div id="slide3"></div>
<div id="slide2"></div>
<div id="slide1"></div>
Any help with this would be great.
Change $count=1; to $count = sizeof($arrayreversed);
and $count++; to $count--;
First of all get the amount of keys in the array and assign it as $count, like this:
$count = count($arrayreversed);
Then replace the $count++ with $count-- and you're all set.
How about using the size of the array as your initial count and then subtracting 1 each iteration?
<?php $arrayforward = get_field('background_slider');
$arrayreversed = array_reverse($arrayforward);
$count=count($arrayreversed);
foreach($arrayreversed as $subarray) {
$subfield1 = $subarray['background_image'];
$subfield2 = $subarray['background_image_alt'];
$subfield3 = $subarray['text_image'];
$subfield4 = $subarray['text_image_alt']; ?>
<div id="slide<?php echo $count; ?>" class="contentslider_wrapper">
<div class="fullpageimage">
<img id="slideImg<?php echo $count; ?>" src="<?php echo $subfield1; ?>" alt="<?php the_sub_field('background_image_alt'); ?>" />
<img id="textImg<?php echo $count; ?>" class="slidetext" src="<?php echo $subfield2; ?>" alt="<?php the_sub_field('text_image_alt'); ?>" />
</div>
</div>
<?php $count--; }; ?>

Quick php question about ordering row results

Consider the following :
$img_pathm = JURI::root().'images/properties/images/'.$this->datos->id.'/';
$peque_path = JURI::root().'images/properties/images/thumbs/'.$this->datos->id.'/';
$result = count($this->Images);
$resultf = $result-1;
while ($resultf>=0){ ?>
<span class="editlinktip hasTip" title="<?php echo $this->datos->image1;?>::
<img border="1" src="<?php echo $peque_path.$this->Images[$resultf]->name; ?>" name="imagelib" alt="<?php echo JText::_( 'No preview available'.$img_pathm ); ?>" width="206" height="100" />">
<img src="<?php echo $peque_path.$this->Images[$resultf]->name; ?>" alt="Additional image <?php echo $resultf+1 ?>" width="65px" height="50px"/></span> <?php
$resultf--; }
This currently prints images one after the other. All I need to do is to invert the order in which these images are printed to the user. I am not sure where, or how I would insert something similar to ORDER by in this code? Thanks in advance!
Just loop the other way:
$img_pathm = JURI::root().'images/properties/images/'.$this->datos->id.'/';
$peque_path = JURI::root().'images/properties/images/thumbs/'.$this->datos->id.'/';
$result = count($this->Images);
$resultf = 0;
while ($resultf<$result){ ?>
<span class="editlinktip hasTip" title="<?php echo $this->datos->image1;?>::
<img border="1" src="<?php echo $peque_path.$this->Images[$resultf]->name; ?>" name="imagelib" alt="<?php echo JText::_( 'No preview available'.$img_pathm ); ?>" width="206" height="100" />">
<img src="<?php echo $peque_path.$this->Images[$resultf]->name; ?>" alt="Additional image <?php echo $resultf+1 ?>" width="65px" height="50px"/></span> <?php
$resultf++; }
Maybe you can use array_reverse

Categories