0

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:

enter image description here

<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>
0

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();
| improve this answer | |
  • Thanks Carlos full my code is top edit. You can do this in my code!? – Beri Aug 21 '18 at 10:07
  • Na ;D Just try to do it yourself and put the code in pastebin.com if you still can't get it... – Carlos Faria Aug 27 '18 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.