【问题标题】:Have an ad space repeat every 10 posts [duplicate]每 10 个帖子重复一个广告空间 [重复]
【发布时间】: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 广告是有限制的
  • &lt;?php 我的口味太多了..
  • if($count%10==0) == if (($count % 10) == 0) ?
  • @Fred - 是的....我只是添加了额外的括号以使其更清晰;虽然使用空格确实使它更具可读性

标签: php wordpress


【解决方案1】:
     <?php if (have_posts()) : ?>
    <?php $count = 0; ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php $count++; ?>
    //////////////////////
   <?php  if($count%10==0){  echo "</br></br></br>"; }   ?>
    ///////////////////

      <?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; ?>

【讨论】:

  • 如果我放任何东西而不是 ,它就可以工作,除了广告代码。如果我把那个wordpress告诉我:解析错误:语法错误,意外的T_STRING,期待','或';'在这一行 "; } ?>
  • @user2839612 将所有&lt;/br&gt;&lt;/br&gt;&lt;/br&gt; 更改为&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt; - &lt;/br&gt; 无效,会导致错误。
  • 我删除了它并放了广告的代码,但它给了我这个错误
猜你喜欢
  • 2015-08-14
  • 2011-10-09
  • 2020-06-03
  • 1970-01-01
  • 2021-04-17
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
  • 2019-10-18
相关资源
最近更新 更多