dongqiulei6805 2017-12-23 16:41
浏览 24
已采纳

如何在三个帖子后显示横幅?

Below is the code to display the post. That loads every 10 posts. And I want to display banner after 3 post. Can someone tell me, code to display banner after 3 post? By completing the code below. Thank you so much for your help.

<?php
        $stories = Wo_GetPosts(array('limit' => 10));
        if (count($stories) <= 0) {
          echo Wo_LoadPage('story/no-stories');
        } else {
          foreach ($stories as $wo['story']) {
            echo Wo_LoadPage('story/content');
          }
        }
        ?>
  • 写回答

2条回答 默认 最新

  • doudeng2184 2017-12-26 14:30
    关注

    You are trying to use count() in a way that this function is not intended. Per the PHP documentation:

    Counts all elements in an array, or something in an object. http://php.net/manual/en/function.count.php

    What you need to do is create a counter variable that increments within your foreach loop, and when it hits 3 outputs the banner. Your code might look something like this:

    <?php
    
      $stories = Wo_GetPosts(array('limit' => 10));
    
      // No stories; output error
      if (count($stories) <= 0) 
        echo Wo_LoadPage('story/no-stories');
    
      // Stories exist; show them!
      } else {
    
        $count = 0;
    
        // Loop through $stories
        foreach ($stories as $wo['story']) {
          // Increment the value of $count by +1
          $count++; 
    
          if ($count == 3) {
            // Output my Banner here
          }
    
          echo Wo_LoadPage('story/content');
        }
      } 
    
    ?>
    

    One thing I would note is that the above code only outputs a banner one time; when the value of $count is 3. You could adjust this to run ever 3rd story by changing the match from if ($count ==3) to if ($count % 3 == 0) which essentially reads as If the value of $count is divisible by 3.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题