在wordpress循环中的第三项之后插入代码


Insert code after the third item in the wordpress loop

我正试图找到一种方法,在wordpress循环中的x项之后注入一些代码。因此,例如,这可以是在页面上显示的每三个帖子后添加一个广告。

这在PHP中可能吗?

如果您的WP循环看起来像这样:

if (have_posts()) :
   while (have_posts()) :
      the_post();
      the_content();
   endwhile;
endif;

你必须做一些类似的事情:

$i = 0;
if (have_posts()) :
   while (have_posts()) :
      $i++;
      if (($i % 3) == 0) :
        echo 'This the third post';
      endif;
      the_post();
      the_content();
   endwhile;
endif;