skip to Main Content

I need a function that after three posts give me my add then after four posts give my add fifth then after eight posts give new my add then a big banner in the middle then again the same scene the same series starts. Go to the loop and then down to the pagination ETC…

Post1   Post2   Image
Post4   Post5   Post6
Image   Post8   Post9
Post10   Post11   Post12
Post13   Post14   Image

Big Image

Post16   Post17   Image
Post19   Post20   Post21
Image   Post23   Post24

Pagination 1 2 3 4 5 ….

Like this Help Me enter image description here

2

Answers


  1. Chosen as BEST ANSWER
    <?php
    $query = $GLOBALS['wp_query'];
    $ads = [
        '<div class="post-ad"><a href="enter-url-here"><img src="ad-image-here" /></a></div>',
        '<div class="post-ad"><a href="enter-url-here"><img src="ad-image-here" /></a></div>',
        '<div class="post-ad"><a href="enter-url-here"><img src="ad-image-here" /></a></div>',
        // and so on
      ];
      $count = 0; 
      $ad_positions = [3, 7, 11, 15]; // enter the desired positions
    if ( have_posts() ) :
        $nuxy_mod_option = apply_filters( 'stm_me_get_nuxy_mod', 'list', 'listing_view_type' );
        if ( wp_is_mobile() ) {
            $nuxy_mod_option = apply_filters( 'stm_me_get_nuxy_mod', 'grid', 'listing_view_type_mobile' );
        }
        if ( function_exists( 'stm_is_multilisting' ) && stm_is_multilisting() && wp_is_mobile() ) {
            $mltSlug         = get_query_var( 'post_type' );
            $nuxy_mod_option = apply_filters( 'stm_me_get_nuxy_mod', 'grid', $mltSlug . '_view_type_mobile' );
        }
        $view_type = apply_filters( 'stm_listings_input', $nuxy_mod_option, 'view_type' );
        /*Filter Badges*/
        do_action( 'stm_listings_load_template', 'filter/badges' );
        if ( boolval( apply_filters( 'is_listing', array() ) ) && 'sold_car' !== $type ) {
            do_action( 'stm_listings_load_template', 'classified/filter/featured' );
        }
        ?>
        <div class="stm-isotope-sorting stm-isotope-sorting-<?php echo esc_attr( $view_type ); ?>">
            <?php
            if ( 'grid' === $view_type ) :
                ?>
            <div class="row row-3 car-listing-row car-listing-modern-grid">
                <?php
            endif;
                $template = 'partials/listing-cars/listing-' . $view_type . '-loop';
            if ( apply_filters( 'stm_is_ev_dealer', false ) || boolval( apply_filters( 'is_listing', array( 'listing', 'listing_two', 'listing_two_elementor', 'listing_three', 'listing_three_elementor', 'listing_one_elementor' ) ) ) || apply_filters( 'stm_is_dealer_two', false ) || apply_filters( 'stm_is_listing_five', false ) || apply_filters( 'stm_is_listing_six', false ) ) {
                $template = 'partials/listing-cars/listing-' . $view_type . '-directory-loop';
            } elseif ( apply_filters( 'stm_is_listing_four', false ) ) {
                $template = 'partials/listing-cars/listing-four-' . $view_type . '-loop';
            } elseif ( apply_filters( 'stm_is_boats', false ) && 'list' === $view_type ) {
                $template = 'partials/listing-cars/listing-' . $view_type . '-loop-boats';
            } elseif ( apply_filters( 'stm_is_motorcycle', false ) ) {
                $template = 'partials/listing-cars/motos/' . $view_type;
            } elseif ( apply_filters( 'stm_is_aircrafts', false ) ) {
                $template = 'partials/listing-cars/listing-aircrafts-' . $view_type;
            }
            while ( have_posts() ) :
                the_post();
                get_template_part( $template );     
                $count++;
                if( in_array( $count, $ad_positions ){
                    $ad = array_shift( $ads ); // gets and removes the first ad from the array
                    echo $ad;
                } )
                endwhile;
            if ( 'grid' === $view_type ) :
                ?>
            </div>
        <?php endif; ?>
        </div>
        <?php
        if ( is_null( $navigation_type ) || 'pagination' === $navigation_type ) :
    stm_listings_load_pagination();
        else :
            $ppp   = $query->query['posts_per_page'];
            $paged = ( ! empty( $query->query['paged'] ) && 1 !== $query->query['paged'] ) ? $query->query['paged'] + 1 : 2;
            if ( $ppp < $query->found_posts ) {
                echo "<a class='btn stm-inventory-load-more-btn' href='#' data-ppp='" . esc_attr( $ppp ) . "' data-page='" . esc_attr( $paged ) . "' data-nav='load_more' data-offset='1'>" . esc_html__( 'Load More', 'motors' ) . '</a>';
            }
        endif;
        wp_reset_query(); //phpcs:ignore
        ?>
    <?php else : ?>
        <h3><?php esc_html_e( 'Sorry, no results', 'motors' ); ?></h3>
    <?php endif; ?>
    <?php if ( apply_filters( 'stm_is_aircrafts', false ) ) : ?>
        <script>
    jQuery(document).ready(function (){
                var showing = '<?php echo esc_html( $query->found_posts ); ?>';
                jQuery('.ac-total').text('<?php echo esc_html( $query->found_posts ); ?>');
                if(showing === '0') {
    jQuery('.ac-showing').text('0');
                }
            });
        </script>
    <?php endif; ?>
    

  2. You’ll need to add code to the loop in your theme. If this is your home page, then you would edit the loop in index.php. If it’s a category page or you want it to apply to those pages too, you’ll edit the loop in archive.php (assuming you don’t have a template file targeting categories, in which case you’d edit it there as well).

    The start of the loop will look something like (or exactly) like this:

    <?php if (have_posts()): while (have_posts()) : the_post(); ?>
    

    Before this line, add an array containing the HTML for your ads, like this:

    <?php 
      $ads = [
        '<div class="post-ad"><a href="enter-url-here"><img src="ad-image-here" /></a></div>',
        '<div class="post-ad"><a href="enter-url-here"><img src="ad-image-here" /></a></div>',
        '<div class="post-ad"><a href="enter-url-here"><img src="ad-image-here" /></a></div>',
        // and so on
      ];
    ?>
    

    Also before the loop starts, add a counter variable and an array of the positions you want the ads to show in:

    <?php 
      $count = 0; 
      $ad_positions = [3, 7, 11, 15]; // enter the desired positions
    ?>
    

    Inside the loop, add this code that increments the counter variable, and outputs ads based on the counter variable:

    <?php
      $count++;
      if( in_array( $count, $ad_positions ) ){
        $ad = array_shift( $ads ); // gets and removes the first ad from the array
        echo $ad;
      } 
    ?>
    

    So your while loop will be this (now that I have that code to look at:

    while ( have_posts() ) :
      the_post();
                    
      $count++;
      if( in_array( $count, $ad_positions ) ){
        $ad = array_shift( $ads ); // gets and removes the first ad from the array
        echo $ad;
      }
      get_template_part( $template ); 
    endwhile;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search