Php 高级自定义字段转发器-如何颠倒顺序(WordPress)
我正在使用高级自定义字段转发器插件创建一个带有链接的大型图像网格。我想在顶部创建最新的项目,所以我要问的是如何通过代码反转顺序 任何帮助都会很好。这是我目前的代码Php 高级自定义字段转发器-如何颠倒顺序(WordPress),php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,我正在使用高级自定义字段转发器插件创建一个带有链接的大型图像网格。我想在顶部创建最新的项目,所以我要问的是如何通过代码反转顺序 任何帮助都会很好。这是我目前的代码 <?php if( have_rows('shop_product') ): while ( have_rows('shop_product') ) : the_row(); ?> <div class="shop-ite
<?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;
?>
">
使用输出缓冲区,这不是最干净的方式
<?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);
?>
">
您可以使用获取字段(“我的中继器”)
将中继器字段作为一个数组获取,您可以循环使用
然后使用PHP的array\u 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:row/column reverse
因为可能只需要交换顺序来显示它
例如:
display:flex;
flex-flow:row-reverse;