首页 > 解决方案 > 返回每个帖子的元数组并合并为一个

问题描述

我有一个名为 Boutique 的自定义帖子类型,其中包含一个名为 Products 的转发器字段。

我的循环正在工作,但我希望所有产品都以随机顺序显示。

例子:

精品 A 与产品 : [1,2,3] 精品 B 与产品 : [4,5,6]

我想要一个这样的数组:array([1],[4],[2],[3],[5],[6])

实际上我只能通过随机播放来获得这个:数组([4,5,6],[1,2,3])

我应该如何将所有产品合并到一个数组中,这样我就可以随机播放并循环播放?

$boutiques = get_posts(array(
    'fields'          => 'ids',
    'posts_per_page'  => -1,
    'post_type' => 'boutique'
));
shuffle($boutiques);

 foreach ( $boutiques as $boutique_id ) {

          if( have_rows('boutique_products', $boutique_id) ):
      while  ( have_rows('boutique_products', $boutique_id) ): 
        the_row(); ?>

        <div class="boutique_product">
          <a href="<?php 
   echo esc_url( $boutique_url ); ?>">
            <img src="<?php 
   the_sub_field('product_img'); ?>">
          </a>
        </div>

      <?php  
   endwhile; else :
  endif; ?>

标签: phpwordpress

解决方案


推荐阅读