【发布时间】:2013-10-10 16:56:52
【问题描述】:
我想在我的帖子列表中每十个帖子放置一个 AdSense 方形 (250x250) 广告。
我尝试将此代码添加到我的 index.php 中,在帖子 div 中:
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 11) : ?>
Ad code is here
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php else : ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
它显示正确,但由于某种原因,所有帖子的格式都发生了变化。
另外,此代码仅在第 10 条帖子之后放置广告,在第 20 条之后不会自动重复,等等。
这实际上并不重要,我可以重复这些数字。
【问题讨论】:
-
模运算符
if (($count % 10) == 0) -
您可以在一个页面上放置多少个 AdSense 广告是有限制的
-
<?php我的口味太多了.. -
if($count%10==0)==if (($count % 10) == 0)? -
@Fred - 是的....我只是添加了额外的括号以使其更清晰;虽然使用空格确实使它更具可读性