Advertisement in Loop Posts wordpress

I'm new here I want some help from you in terms of coding in wordpress. The point is that I want to make advertising between items as shown below:

div class="row top-articles"
                ?php 

                        $args= array(
                            'posts_per_page'=4,
                            'post__not_in'= $excluded
                            );
                            $i=0;
                            $otherLoop = new WP_Query( $args );
                            while ($otherLoop - have_posts() ) : $otherLoop-the_post();$i++;
                            $cat = get_the_category($post-ID)[0];
                            array_push($excluded,$post-ID);
                        ?
div class="small-6 top-story-container columns medium-6 large-3"
article class="artikulli-?php echo$cat-slug; ? live"
a href="?php the_permalink();?"
div class="img-container"
div class="inner"

div class="gradient"/div
?php if($videos = get_post_meta($post-ID, 'ecpt_videos','ecpt_foto', true)): ?
i class="icon dotie-icon_video dotie-icon_gallery phone-medium tablet-large desktop-medium"/i
?php endif; ?

?php if($foto = get_post_meta($post-ID, 'ecpt_foto', true)): ?
i class="icon dotie-icon_gallery phone-medium tablet-large desktop-medium"/i
?php endif; ?

?php the_post_thumbnail('250x140'); ?
/div
/div
div class="article-meta equalise"
 h3
span class="underline"

?php if($live = get_post_meta($post-ID, 'ecpt_live', true)): ?
span class="live"Live/span
?php endif; ?

?php echo wp_trim_words( get_the_title(), 7 );?
/span
/h3
span class="datetime primary-color"
?php echo $cat-cat_name; ?
/span
/div
div class="clearfix"/div
/a
/article
/div
?php endwhile;  ?
/div
/div

div class="clearfix"/div

Topic loop ads php Wordpress

Category Web


Use WP_Query and then loop the results using break when you want to stop to put the ad.

$args = [                   
  'posts_per_page' => 5
];

$posts = new WP_Query($args);

Then loop 2

<section class="posts">
<?php $cont = 0; ?>
<?php
  while( $posts->have_posts() ) : $posts->the_post(); $cont++;        
    $id = get_the_ID();
    //Show your post here
    if($cont >= 1) break;
  endwhile;
  ?>
</section>

<div class="ad">
<?php //Show ad ?>
</div>

<?php //Rest of the posts ?>
<section class="posts">
<?php
  while( $posts->have_posts() ) : $posts->the_post();     
    $id = get_the_ID();
    //Show your post here
  endwhile;
  ?>
</section>

If you are doing this in a Post or Page you should reset the query after showing the posts:

wp_reset_postdata();

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.