Альтернативная разметка для каждых 6 сообщений - в Wordpress

Я хочу вставить и код рекламы после того, как каждые 6 разместят в моем блоге. Я не могу действительно выяснить, как убежать из foreach и вставить код рекламы.

1
задан Thomas 11 June 2010 в 18:47
поделиться

2 ответа

Эта ссылка поможет вам. Третий заголовок гласит: 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) : ?>
2
ответ дан 2 September 2019 в 23:48
поделиться

Как прокомментировал @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; ?>
0
ответ дан 2 September 2019 в 23:48
поделиться
Другие вопросы по тегам:

Похожие вопросы: