カスタムフィールド(Wordpress)から画像を取得し、1行に3つの画像を表示したいと思います。さらに、私は追加したいと思います
<ul>
<li>image1</li>
<li>image2</li>
<li>image3</li>
</ul>
<ul>
<li>image4</li>
<li>image5</li>
<li>image6</li>
</ul>
私はいくつかのコードを見つけ、これを達成するためにいくつかのカスタムコーディングを試しました。</ul>
ほとんど動作しましたが、最後に取得した画像の後にタグが必要です。以下のコードは</ul>
、最後に取得された画像の直前にタグを生成します...さらに、私は配列とループにあまり精通していないため、以下のコードよりも簡単にこれを実現できる可能性があります。
誰が私を助けることができますか?何年もありがとうございました!
<?php
// vars
$images = array();
$row = 0;
$i = 0;
$gallery_images = get_field('galerie' );
// loop through gallery images and sort into the $images array
if( $gallery_images )
{
foreach( $gallery_images as $image )
{
// Insert the url tag
if ( $i === 0 )
{
echo "<ul class='galerieul'>";
}
// increase $i
$i++;
// Insert the url tag
if( $i > 3 )
{
echo "<ul class='galerieul'>";
}
// if $i has increased above 3, increase the row and reset $i
if( $i > 3 )
{
$i = 1;
$row++;
}
// add image to row
$images[ $row ][] = $image;
?>
<li class="customimagestyle">
<?php echo $image['title']; ?><br />
<a href="<?php echo $image['url']; ?>" rel="lightbox"><img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /></a><br />
<?php echo $image['description']; ?>
</li>
<?php
// Insert the url tag
if ( $i === 3 )
{
echo "</ul>";
}
if ($i < $row)
{
echo '</ul>';
}
}
}
?>