php - Adding a post of certain category every nth post -
i have 2 arrays posts, $portfolio_items , $ad_items. every third post should post $ad_items. posts $ad_items can repeat until posts $portfolio_items have been displayed.
i'm kind of novice when comes php, i'm having hard time wrapping head around problem. pointers , tips appreciated. thanks!
edit: managed solve it, feeling code clumsy , bloated.
<?php $counter = 1; $rotation = 3; $ad_counter = 0; $ad_id = array(); $query1 = new wp_query(array( "post_type" => "post", "post_status" => "publish", "tax_query" => array( array( "taxonomy" => "category", "field" => "slug", "terms" => "test", "operator" => "not in" ) ) )); $query2 = new wp_query(array( "post_type" => "post", "post_status" => "publish", "tax_query" => array( array( "taxonomy" => "category", "field" => "slug", "terms" => "test" ) ) )); if($query2 -> have_posts()) { while($query2 -> have_posts()) : $query2 -> the_post(); array_push($ad_id, get_the_id()); endwhile; } if($query1 -> have_posts()) { while($query1 -> have_posts()) { if($counter == $rotation) { if($ad_counter >= count($ad_id)) { $ad_counter = 1; } else { $ad_counter++; } $t = get_post($ad_id[$ad_counter-1]); ?> <div class="post type-post status-publish format-standard hentry red"> <h1> <?php echo $t->post_title; ?> </h1> <p> <?php $t->post_content; ?> </p> </div> <?php $counter = 0; } else { $query1 -> the_post(); get_template_part("template/testpost"); } $counter++; } wp_reset_postdata(); } ?>
this typical modulo task. here pseudo code:
for ($i=0; $i<20; $i++) { if (($i % 3) == 0) // sth. else // sth. else or don't @ }
Comments
Post a Comment