Show only first 9 images ACF Gallery and trigger gallery from link - foreach

I am trying to show the first 9 images as previews in an ACF gallery. When clicking one, a lightbox opens and I would like all the images, including the initial 9 to be included
I can only find solutions to show 1 image not more than 1
I tried this
<?php $images = get_field( 'gallery' ); ?>
<?php $image = $images[9]; ?>
<?php if ( $images ) : ?>
<?php foreach ( $images as $gallery_image ): ?>
<a href="<?php echo esc_url( $image['url'] ); ?>">
<img src="<?php echo esc_url( $image['sizes']['medium'] ); ?>" alt="<?php echo esc_attr( $image['alt'] ); ?>" />
</a>
<p><?php echo esc_html( $image['caption'] ); ?></p>
<?php endforeach; ?>
<?php endif; ?>
but this only repeats the 9th image
I also tried this
<?php if ( $gallery_images ) : ?>
<?php foreach(array_slice($gallery_images,0,9) as $gallery_image) ?>
<a href="<?php echo esc_url( $image['url'] ); ?>">
<img src="<?php echo esc_url( $image['sizes']['medium'] ); ?>" alt="<?php echo esc_attr( $image['alt'] ); ?>" />
</a>
<p><?php echo esc_html( $image['caption'] ); ?></p>
<?php endforeach; ?>
<?php endif; ?>
But this doesn't work at all
I would also like to create a button that just opens the gallery without showing any preview image
Essentially I want to trigger a click on the first image when clicking the 'show gallery button'
Any help would be great
edit
I have just tried this:
<div class="listing-preview-gallery">
<?php $gallery_images = get_field( 'gallery' ); ?>
<?php if ( $gallery_images ) : ?>
<?php foreach ( $gallery_images as $gallery_image ): ?>
<?php if ( $i == 1 ) { ?>
<div class="preview-image-container">
<div class="the-image">
<a href="<?php echo esc_url( $gallery_image['url'] ); ?>">
<img src="<?php echo esc_url( $gallery_image['sizes']['medium'] ); ?>" alt="<?php echo esc_attr( $gallery_image['alt'] ); ?>" />
</a>
</div>
</div>
<?php } elseif ( $i == 2 ) { ?>
<div class="preview-image-container">
<div class="the-image">
<a href="<?php echo esc_url( $gallery_image['url'] ); ?>">
<img src="<?php echo esc_url( $gallery_image['sizes']['medium'] ); ?>" alt="<?php echo esc_attr( $gallery_image['alt'] ); ?>" />
</a>
</div>
</div>
<?php } elseif ( $i == 3 ) { ?>
<div class="preview-image-container">
<div class="the-image">
<a href="<?php echo esc_url( $gallery_image['url'] ); ?>">
<img src="<?php echo esc_url( $gallery_image['sizes']['medium'] ); ?>" alt="<?php echo esc_attr( $gallery_image['alt'] ); ?>" />
</a>
</div>
</div>
<?php } elseif ( $i == 4 ) { ?>
<div class="preview-image-container">
<div class="the-image">
<a href="<?php echo esc_url( $gallery_image['url'] ); ?>">
<img src="<?php echo esc_url( $gallery_image['sizes']['medium'] ); ?>" alt="<?php echo esc_attr( $gallery_image['alt'] ); ?>" />
</a>
</div>
</div>
<?php } elseif ( $i == 5 ) { ?>
<div class="preview-image-container">
<div class="the-image">
<a href="<?php echo esc_url( $gallery_image['url'] ); ?>">
<img src="<?php echo esc_url( $gallery_image['sizes']['medium'] ); ?>" alt="<?php echo esc_attr( $gallery_image['alt'] ); ?>" />
</a>
</div>
</div>
<?php } elseif ( $i == 6 ) { ?>
<div class="preview-image-container">
<div class="the-image">
<a href="<?php echo esc_url( $gallery_image['url'] ); ?>">
<img src="<?php echo esc_url( $gallery_image['sizes']['medium'] ); ?>" alt="<?php echo esc_attr( $gallery_image['alt'] ); ?>" />
</a>
</div>
</div>
<?php } elseif ( $i == 7 ) { ?>
<div class="preview-image-container">
<div class="the-image">
<a href="<?php echo esc_url( $gallery_image['url'] ); ?>">
<img src="<?php echo esc_url( $gallery_image['sizes']['medium'] ); ?>" alt="<?php echo esc_attr( $gallery_image['alt'] ); ?>" />
</a>
</div>
</div>
<?php } elseif ( $i == 8 ) { ?>
<div class="preview-image-container">
<div class="the-image">
<a href="<?php echo esc_url( $gallery_image['url'] ); ?>">
<img src="<?php echo esc_url( $gallery_image['sizes']['medium'] ); ?>" alt="<?php echo esc_attr( $gallery_image['alt'] ); ?>" />
</a>
</div>
</div>
<?php } elseif ( $i == 9 ) { ?>
<div class="preview-image-container">
<div class="the-image">
<a href="<?php echo esc_url( $gallery_image['url'] ); ?>">
<img src="<?php echo esc_url( $gallery_image['sizes']['medium'] ); ?>" alt="<?php echo esc_attr( $gallery_image['alt'] ); ?>" />
</a>
</div>
</div>
<?php }
$i++; ?>
<?php endforeach; ?>
<?php endif; ?>
</div>
And it successfully only previews 9, but the rest do not show in the lightbox
I tried this for the button click
jQuery ( function($) {
$('.gallery-button').off('touchstart touchend').on('click', function() {
$('.listing-preview-gallery #preview-image-container:first-child .the-image a').trigger('click');
} );
} );
Edit 2
It occurs to me I can use nth child to hide all but the first 9 thumbnails, but then it still loads the images - I wonder if there is a way to not load the remaining images until the lightbox is opened

So I don't have the answer to your full question, but your php example to show the first 9 photos, there's a MUCH shorter way to write that out. Take a look at the code below.
<div class="listing-preview-gallery">
<?php $gallery_images = get_field( 'gallery' );
if ( $gallery_images ) :
$previewCount = 9;
$i = 0;
foreach ( $gallery_images as $gallery_image ):
$i++;
if($i <= $previewCount){ ?>
<div class="preview-image-container">
<div class="the-image">
<a href="<?php echo esc_url( $gallery_image['url'] ); ?>">
<img src="<?php echo esc_url( $gallery_image['sizes']['medium'] ); ?>" alt="<?php echo esc_attr( $gallery_image['alt'] ); ?>" />
</a>
</div>
</div>
<?php }
endforeach;
endif; ?>
</div>
You would just set the $previewCount variable to whatever number of images you want to preview, and the code will do the rest. It uses $i as a counter, and checks against the $previewCount variable to determine when to stop looping through the $gallery_images.

Related

ACF Post object Is not pulling the Correct Featured image

I am using ACF post object field to display related recipes on posts.
I have used below code to make it happen, its pulling correct posts and title on the front end. But featured image instead of displaying the selected related post images, its displaying the main post featured image to all the related posts.
<?PHP
$featured_posts = get_field('related_posts');
if( $featured_posts ): ?>
<ul>
<?php foreach( $featured_posts as $featured_post ):
$permalink = get_permalink( $featured_post->ID );
$title = get_the_title( $featured_post->ID );
$featured_image = get_the_post_thumbnail_url($document->ID);
$custom_field = get_field( 'field_name', $featured_post->ID );
?>
<li><img src="<?php echo esc_html( $featured_image ); ?>" class="people-post-image">
<?php echo esc_html( $title ); ?>
<span>A custom field from this post: <?php echo esc_html( $custom_field ); ?></span>
</li>
<?php endforeach; ?>
</ul>
It appears you simply have a misused variable in your code where you create the $featured_image variable. See my edits to your code below, along with a few other improvements.
<?php
$featured_posts = get_field('related_posts');
if( $featured_posts ): ?>
<ul>
<?php foreach( $featured_posts as $featured_post ):
$permalink = get_permalink( $featured_post->ID );
$title = get_the_title( $featured_post->ID );
$featured_image = get_the_post_thumbnail_url($featured_post->ID);
$custom_field = get_field( 'field_name', $featured_post->ID );
?>
<li><img src="<?php echo esc_html( $featured_image ); ?>" class="people-post-image">
<?php echo esc_html( $title ); ?>
<span>A custom field from this post: <?php echo esc_html( $custom_field ); ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php
endif;
Thanks vpjm3.0
I was able to work it out from your suggestion above.
<?php
$featured_posts = get_field('related_posts');
if( $featured_posts ): ?>
<ul>
<?php foreach( $featured_posts as $featured_post ):
$permalink = get_permalink( $featured_post->ID );
$title = get_the_title( $featured_post->ID );
$featured_image = get_the_post_thumbnail( $value->ID, 'full');
$custom_field = get_field( 'field_name', $featured_post->ID );
?>
<li><img src="<?php echo esc_html( $featured_image ); ?>" class="people-post-image">
<?php echo esc_html( $title ); ?>
<span>A custom field from this post: <?php echo esc_html( $custom_field ); ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Yii2 generate 3 column table using foreach

how do i generate only 3 columns in a page using below code
if i keep echo'(table)' inside it generates the whole table so i want only one table
allow only 3 columns in a page after 3 columns append a new row
<?php
$i = 0;
echo '<table style="float: left;width: -weekbit-fill-available"; ><tr>';
?>
<?php foreach ($product as $p) {
$i++;
// $i++;
echo "<tr><td>Name:<b>". $p->name ."</td></tr>";
echo "<tr><td>productID:<b>".$p->proid ."</td></tr>";
echo "<tr><td>Price:<b>". $p->price ."</td></tr>";
echo "<tr><td><a href='/cart/cart/add?id=$p->proid'><input type='button' value='addtocart'/></td></tr>";
if($i==3){
echo '</tr><tr>';
}
echo"</tr>
</table>";
?>
<?php }?>
<?php echo"</tr>
</table>"?>
thanks in advance
Try below
<table style="float: left; width: -weekbit-fill-available">
<tr>
<th>Name</th>
<th>ProductID</th>
<th>Price</th>
<th>Action</th>
</tr>
<?php foreach ($product as $p) : ?>
<tr>
<td><?= $p->name ?></td>
<td><?= $p->proid ?></td>
<td><?= $p->price ?></td>
<td colspan="3"><?= \yii\helpers\Html::a('addtocart',
['/cart/cart/add', 'id' => $p->proid],
[
'title' => 'Add to Cart',
'class' => 'btn btn-warning btn-sm',
]); ?></td>
</tr>
<?php endforeach; ?>
</table>
Use <tr> for each row and <td> for each column
<table style="float: left; width: -weekbit-fill-available"; >
<?php foreach ($product as $p): ?>
<tr>
<td>Name: <b><?= $p->name ?></td>
<td>productID: <b><?= $p->proid ?></td>
<td>Price: <b><?= $p->price ?></td>
<td><a href='/cart/cart/add?id=<?= $p->proid ?>'><input type='button' value='addtocart'/></td>
</tr>
<?php endforeach; ?>
</table>";
See also:
HTML tables https://www.w3schools.com/html/html_tables.asp
Yii2 GridView https://www.yiiframework.com/doc/guide/2.0/en/output-data-widgets

How do I start the ouput from the second iteration of a foreach loop?

I have the following code output every iteration of the object. How do I make the output start from the second iteration?
<?php foreach ($data['restos'] as $lid => $resto):?>
<?php
$time_arr = sic_get_start_expire_date_for_user_resto($this->current_user->ID, $lid);
$is_expired_class = '';
if (isset($time_arr['expire_time']) && time()>strtotime( $time_arr['expire_time'] ) ){
$is_expired_class = 'sic-expired-resto';
}
?>
<?php if (!empty($data['restos_metas']['sic_restos_on']) && !empty($level['resto_image_url'])):?>
<div class="resto-outer-wrapper <?php echo $is_expired_class;?>"><img src="<?php echo $resto['resto_image_url'];?>" class="resto" title="<?php echo $resto['label'];?>" /></div>
<?php elseif (!empty($resto['label'])):?>
<div class="sic-above <?php echo $is_expired_class;?>"><?php echo $resto['label'];?></div>
<?php endif;?>
<?php endforeach;?>
The easiest way would be to enclose the output in
if ($uniqueVar) {
// OUTPUT
} else {
$uniquVar = true;
}
Which for your code would look like:
<?php foreach ($data['restos'] as $lid => $resto):?>
<?php
$time_arr = sic_get_start_expire_date_for_user_resto($this->current_user->ID, $lid);
$is_expired_class = '';
if (isset($time_arr['expire_time']) && time()>strtotime( $time_arr['expire_time'] ) ){
$is_expired_class = 'sic-expired-resto';
}
?>
//----------------------------------
<?php if ($notFirstIteration):?>
//----------------------------------
<?php if (!empty($data['restos_metas']['sic_restos_on']) && !empty($level['resto_image_url'])):?>
<div class="resto-outer-wrapper <?php echo $is_expired_class;?>">
<img src="<?php echo $resto['resto_image_url'];?>" class="resto" title="<?php echo $resto['label'];?>" />
</div>
<?php elseif (!empty($resto['label'])):?>
<div class="sic-above <?php echo $is_expired_class;?>"><?php echo $resto['label'];?>
</div>
<?php endif;?>
//--------------------------------
<?php else:?>
<?php $notFirstIteration = true;?>
<?php endif;?>
//--------------------------------
<?php endforeach;?>
This will be true for the second iteration and onward.

php foreach inside foreach: what is the error?

I want the code to be executed only if i>10. Can you please say what the error is?
<?php $data = wp_excel_cms_get("top100"); ?>
<?php foreach($data as $entry):
{
foreach($entry as $entry[i>10]):
<?php echo $entry[0]." ";?><?php echo $entry[1];?><br />
}
?>
<hr />
<?php endforeach; ?>

Flexslider not syncing with dynamic items

I have flexslider activated with a ACF for each loop using this code:
$(window).load(function() {
// The slider being synced must be initialized first
$('#gallerycarousel').flexslider({
animation: "slide",
controlNav: false,
animationLoop: false,
slideshow: false,
directionNav: true,
itemWidth: 300,
itemMargin: 10,
animationLoop: true,
asNavFor: '#galleryslider'
});
$('#galleryslider').flexslider({
animation: "slide",
controlNav: false,
animationLoop: false,
slideshow: false,
directionNav: true,
sync: "#gallerycarousel"
});
});
<div class="flexslider" id="galleryslider">
<ul class="slides">
<?php
$gallery = get_field('fullgallery');
foreach( $gallery as $galleryImage ):
$image = $galleryImage['url']; ?>
<li style="background-image: url('<?php echo $image; ?>')"></li>
<?php endforeach; ?>
</ul>
</div>
<div class="flexslider" id="gallerycarousel">
<ul class="slides">
<?php
foreach( $gallery as $galleryImage ):
$image = $galleryImage['url'];
?>
<li style="background-image: url('<?php echo $image; ?>'); background-size: cover;">
<div class="viewImg">
<a class="fancybox" rel="gallery1" href="<?php echo $image; ?>" title="<?php echo $galleryImage['caption']; ?>"><i class="fa fa-search"></i></a>
</div>
</li>
<meta property="og:image" content="<?php echo $galleryImage['url']; ?>" />
<?php endforeach; ?>
</ul>
</div>
My issue is that on click of a thumbnail, it takes me to the wrong large image, I have tried re-downloading flexslider but no luck, I have also tried replacing the for each loop with just static items and, that seems to fix it.
I am unsure on how to get it to work with the foreach loop, does anyone have a solution for this?
It seems removing this: <meta property="og:image" content="<?php echo $galleryImage['url']; ?>" /> fixed the issue.

Resources