よくあるパターンですが、WordPressの投稿ループの間に広告を挿入する方法です。
3の倍数毎に広告を挿入する
<?php
global $wp_query;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'template/content' );
if(($wp_query->current_post + 1) % 3 === 0){
// ここに広告コード
}
endwhile;
else :
get_template_part( 'template/content', 'none' );
endif;
?>
3番目の投稿の次にのみ広告を挿入する
<?php
global $wp_query;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'template/content' );
if(($wp_query->current_post + 1) == 3){
// ここに広告コード
}
endwhile;
else :
get_template_part( 'template/content', 'none' );
endif;
?>
ポイントは
global $wp_query;
でグローバル変数$wp_queryを呼び出して、
$wp_query->current_post
で投稿番号(0始まり)を取得して、指定した数値と照合して使用します。