我正在使用高级自定义字段中继器插件创建一个带有链接的大网格图像。我想要我在顶部创建的最新项目,所以我要问的是如何通过代码反转订单?
任何帮助都会很棒。这是我当前的代码
<?php if( have_rows('shop_product') ):
while ( have_rows('shop_product') ) : the_row();
?>
<div class="shop-item">
<div class="image-hover">
<div class="image-hover-inner">
<img src="<?php the_sub_field('product_image');?>">
<div class="image-caption">
<div class="image-caption-inner">
<a href="<?php the_sub_field('product_url');?>" target="_blank"><?php the_sub_field('product_brand');?><br>
Buy Now</a>
</div>
</div>
</div>
</div>
</div>
<?php endwhile;
else :
endif;
?>
您可以使用get_field('my_repeater')
将中继器字段作为一个数组进行循环。
然后使用PHP的array_reverse()
函数,可以得到一个可以循环的反向数组。
唯一的区别是必须将字段作为数组键访问,而不是使用ACF的sub_field
函数。
例如:
<?php $products = array_reverse(get_field('shop_product'));
foreach ($products as $product): ?>
<img src="<?php echo $product['product_image']; ?>">
<a href="<?php echo $product['product_url']; ?>">Buy now</a>
<?php endforeach; ?>
它可能不能回答实际问题,但可以解决问题。如果你正在显示中继器的行,你可以使用CSS display:flex和flex flow:行/列反向;
由于可能只需要交换订单即可显示
示例:
display:flex;
flex-flow:row-reverse;
在我的脑海中,使用输出缓冲区-这不是中最干净的方式
<?php $outs = array(); if( have_rows('shop_product') ):
while ( have_rows('shop_product') ) : the_row(); ob_start();
?>
<div class="shop-item">
<div class="image-hover">
<div class="image-hover-inner">
<img src="<?php the_sub_field('product_image');?>">
<div class="image-caption">
<div class="image-caption-inner">
<a href="<?php the_sub_field('product_url');?>" target="_blank"><?php the_sub_field('product_brand');?><br>
Buy Now</a>
</div>
</div>
</div>
</div>
</div>
<?php $outs[] = ob_get_clean(); endwhile;
else :
endif;
$outs = array_reverse($outs);
echo implode($outs);
?>