在 WordPress 循环中每 4 次 post

Get on every 4th post in WordPress loop

我目前正尝试在我的类别页面上创建一个功能,在每 4 次预览 post 中,在类别中显示一个广告块。

基本它将按以下方式工作:

  1. post 1
  2. post 2
  3. post 3
  4. post 4

屏蔽广告

  1. Post 5
  2. Post 6

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

我有以下内容:

<?php $i = 0; ?>
<?php if (post_num ($i%4 == 0)  < (5 - $featured_count)) : echo "this works"; endif ?>

谁能指导我正确的方向

你或多或少在那里,这里有一个简单的例子,只显示一些任意文本或 post 标题:

<?php
    $counter = 0;

    if (have_posts()) {
        while (have_posts()) {
            $counter++;
            the_post();

            if ($counter % 5 === 0) { 
                echo 'Advert Here!';
            } else {
                the_title();
            }
        }
    }
?>

我还没有测试过这个,只是给你一个想法。

编辑:请注意,由于您要在 之后插入广告,因此第四个 post、$counter % 4 不会执行您的操作想一想,如果你想展示四个 post 然后是广告,它会被放在第 5 个 "position",因此 $counter % 5.