Я хочу вставить и код рекламы после того, как каждые 6 разместят в моем блоге. Я не могу действительно выяснить, как убежать из foreach и вставить код рекламы.
Эта ссылка поможет вам. Третий заголовок гласит: Insert Ads After The First Post
Измените код на 6 там, где написано 2:
<?php if (have_posts()) : ?> // Here we check if there are posts
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?> // While we have posts, add 1 to count
<?php if ($count == 6) : ?> // If this is post 6
//Paste your ad code here
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> // post title
<?php the_excerpt(); ?> // You may use the_content too
<?php else : ?> // If this is not post 6
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
UPDATE: Как заметил Гордон, вы просили код через каждые 6 постов (извините, я пропустил это при первом прочтении). Поэтому код должен быть таким:
<?php if ($count % 6 == 0) : ?>
Как прокомментировал @Gordon, вот как я бы переделал этот код;
<?php if (have_posts()) : $count = 1; while (have_posts()): ?>
<?php if ($count == 6) : ?>
// Paste your ad code here
<?php $count = 0; endif; ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php $count++; endwhile; endif; ?>