详情页

WordPress 页面随机位置插入 Adsense 广告

时间:2023年09月26日

编辑:佚名

该代码用于在 WordPress 循环内的随机位置输出 Adsense 广告(或任何其他输出块),限制为预设数量的可能位置。
<?php $ad_positions = array(1,2,4); // array of preset possible ad positions
do { $rnd_ad = $ad_positions[rand(0,count($ad_positions)-1)]; } while ($rnd_ad >= $wp_query->post_count);
// to make sure that the random output fits into the available number of posts
?>
<?php if(have_posts()) : while(have_posts()) : the_post(); // start of the loop // ?>
  // some part of the post output here
<?php if( ($wp_query->current_post+1) == $rnd_ad ) : ?>
  // adsense code here
<?php endif; ?>
  // more post output here
<?php endwhile; endif; // end of the loop // ?>
教程完毕!
相关文章
猜你需要