WordPress Insert ads after every 5th post

Question

Hi I want to create a single archive page where every custom post will get displayed on click of a button and after every 5th post I can add an Ad image which can be fetched as another posts type

I can get the currentpost index and able to add an ad on 5th position but I am unable to continue it further.

Here is my code

// WP_Query arguments
$args = array(
    'post_type'              => array( 'trivia' ),
    'post_status'            => array( 'publish' ),
    'order'                  => 'ASC',
    'orderby'                => 'rand',
  'posts_per_page'          => -1,
  //'offset'                 => 1,
  'post__not_in' => array($recentPosts )
);

// The Query
$query = new WP_Query( $args );
$maxPosts = $query->post_count;
$startPost;


$currentPost = $query->current_post;;
$adSpace = $currentPost+6;

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {


        $query->the_post();
        // do something
  //  $currentPost = $startPost+1;
    if ($currentPost < $maxPosts){
            printf ($currentPost."</br>");
            $currentPost++;

            if($currentPost == $adSpace){

                echo "Ad Space</br>";
            }
        }

Can anyone please help.

Thanks in advance

0
Pankaj Kumar 3 months 0 Answers 8 views 0

Leave an answer