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 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染