functions – Insert ad between X number of posts but on specific paginated pages?

Question

I’m inserting an ad in-between every 5th post using the code below but obviously it shows up on every paginated page. Is there a “pagination” function available to show the ad every 5th post AND only if paged 2, paged 7, paged 12, etc? I don’t know if this is possible with pagination.

<?php
  // set up our arguments for our custom query
  $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  $query_args = array(
    'post_type' => 'post',
    'post_status'=>'publish',
    'posts_per_page' => 11,
    'paged' => $paged
  );
  // create a new instance of WP_Query
  $the_query = new WP_Query( $query_args );
?>

<?php $count = 1; ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
    
<div class="post-content"></div>

<?php if ($count == 5) : ?>

<!-------- AD GOES HERE -------->

    <?php endif; $count++; ?>
    
<?php endwhile; ?>

<div id="navigation">
<?php $big = 999999999; // need an unlikely integer
echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'mid_size' => 2,
    'end_size' => 1,
    'total' => $the_query->max_num_pages
) ); ?>
</div>

<?php else: ?>
  <article>
    <h1>Sorry...</h1>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
  </article>
<?php endif; ?>

0
Nate M. 3 weeks 2022-11-10T02:03:57-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse