我有ACF中继器,并使用array_rand()在每个页面加载中显示一个字段,但是我需要按顺序(行1,行2,行3)一一显示字段,因此是随机性,但要按顺序显示。这就是我现在拥有的,无法获取如何下达此订单。
if ( get_field('random_non-random') == 'random' ) {
echo '<div class="all-featured-image landing">';
$rows = get_field('random_featured_image' );
$rand_row = $rows[ array_rand( $rows, 1 ) ];
$rand_row_image = $rand_row['featured_image'];
$image = wp_get_attachment_image_url( $rand_row_image, 'full' );
echo '<img src=" ' . $rand_row_image['url'] .' " alt="' . $rand_row_image['alt'] .'" title="' . $rand_row_image['title'] .'">' ;
echo '</div>';
}
好像我需要在某处使用count($ rows)吗?
UPDATE ===
这是我所做的,并且效果很好:
if ( get_field( 'multiple_featured_images_enable' ) ) {
if ( get_field('random_non-random') == 'random' ) {
echo '<div class="all-featured-image landing">';
$rows = get_field('random_featured_image' );
$rand_row = $rows[ array_rand( $rows, 1 ) ];
$rand_row_image = $rand_row['featured_image'];
$image = wp_get_attachment_image_url( $rand_row_image, 'full' );
echo '<img src=" ' . $rand_row_image['url'] .' " alt="' . $rand_row_image['alt'] .'" title="' . $rand_row_image['title'] .'">' ;
echo '</div>';
} else {
if(!isset($_SESSION['featured_image_key'])){
$_SESSION['featured_image_key'] = 0;
}
echo '<div class="all-featured-image landing">';
$rows = get_field('random_featured_image' );
$row = $rows[ $_SESSION['featured_image_key'] ];
$row_image = $row['featured_image'];
$image = wp_get_attachment_image_url( $row_image, 'full' );
echo '<img src=" ' . $row_image['url'] .' " alt="' . $row_image['alt'] .'" title="' . $row_image['title'] .'">' ;
echo '</div>';
$_SESSION['featured_image_key']++;
if($_SESSION['featured_image_key'] >= count($rows)){
$_SESSION['featured_image_key'] = 0;
}
}
}