Functions – Insert image between X number of posts but on specific paginated pages?

I am putting an image in the middle of every 5th post using the code below but apparently it is visible on every paged page. Is there a “pagination” function available to show the image in every 5th post and only if page 2, page 7, page 12, etc.?

<?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) : ?>

<!-------- IMAGE 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; ?>

edit:
This code works to show image on page 5 but how would I write it to include other pages? That’s where I’m stumped.

<?php if ($count == 5 && is_paged() && get_query_var('paged') == 5 ) : ?>

<!-- IMAGE GOES HERE -->
    
<?php endif; $count++; ?>

Leave a Comment