在自定义循环中每x个帖子插入一个帖子类型


Insert a post type every x posts in custom loop

我希望有人能帮我解决这个问题。

我正在尝试在wordpress中使用两种不同的帖子类型('product'和' suits ')进行循环

下面的代码工作得很好,它输出了两个帖子类型的列表,按最新到旧的顺序排列。

    $loop = new WP_Query( array( 
                'post_type' => array( 'product', 'outfits' ),
                'posts_per_page' => 15
              ) ); 
            $counter = 0;
            ?>
            <?php if ( $loop->have_posts() ) { while ( $loop->have_posts() ) { $loop->the_post();
            $counter++; ?>
            <?php $loop->is_home = false; ?>
            <?php
                $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
                $titulo = get_the_title();
                $content = apply_filters ('the_content', $post->post_content); ?>
              <div class="caja-<?php echo $counter; ?>">
                <?php if ( $post->post_type == "product" ) {            
                    //some stuff
                <?php } else { 
                    //some stuff
                } ?>
            </div>
            <?php } } wp_reset_postdata();

我想做的是在X套衣服之后插入一个产品帖子类型。

我试图合并一个类似的解决方案,我发现在其他问题没有运气。我不是一个php专家,所以任何帮助是感激的

    <?php
    $args = array('post_type'=>'post', 'posts_per_page'=>9, 'category_name'=>'news');
    $posts = get_posts($args);
    $args = array('post_type'=>'testimonials', 'posts_per_page'=>3);
    $testimonials = get_posts($args);
   // see how many of the regular posts you got back //
   $post_count = count($posts);
   // see how many testimonials you got back //
   $testimonial_count = count($testimonials);
   // add them up to get the total result count //
   $total_count = $post_count + $testimonial_count;
   // Loop through the total number of results //
   for($i = 1; $i <= $total_count; $i++){
   // assuming you want to show one testimonial every third post //
   if($i % 3 == 0){
   // this means you're on the a third post, show a testimonial //
    setup_postdata($testimonials[$i]);
   }
   else{
    /** show a regular post */
   setup_postdata($posts[$i]);
   }
  /** and now handle the output */
  ?><h1><?php the_title();?></h1><?php
 } ?>
$x = 3;
$products = get_posts(array(
    'post_type' => 'product',
    'posts_per_page' => -1
));
$outfits = get_posts(array(
    'post_type' => 'outfit',
    'posts_per_page' => -1
));
foreach ($outfits as $num => $outfit) {
    if ( ($num+1) % $x == 0) {
        if (isset($products[($num+1)/$x - 1])) {
            array_splice( $outfits, $num, 0, array($products[($num+1)/$x - 1]) );    
        }
    }
}
foreach ($outfits as $outfit) {
    $posts_id[] = $outfit->ID;
}
$query = new wp_query(array(
    'post_type' => array('outfit', 'product'),
    'post__in' => $posts_id,
    'posts_per_page' => 15,
    'orderby' => 'post__in'
));