php - Insert a post type every x posts in custom loop -
i hoping me problem.
i'm trying make loop in wordpress 2 different post types ('product' , 'outfits')
the code below working fine, outputs list of 2 post types ordered newest older.
$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();
what insert product post type after x number of outfits.
i trying merge similar solution i've found in other question no luck. i'm not php expert appreciated
<?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 regular posts got // $post_count = count($posts); // see how many testimonials got // $testimonial_count = count($testimonials); // add them total result count // $total_count = $post_count + $testimonial_count; // loop through total number of results // for($i = 1; $i <= $total_count; $i++){ // assuming want show 1 testimonial every third post // if($i % 3 == 0){ // means you're on third post, show testimonial // setup_postdata($testimonials[$i]); } else{ /** show regular post */ setup_postdata($posts[$i]); } /** , handle 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 $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 $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' ));
Comments
Post a Comment