【问题标题】:php - foreach loop, custom slidephp - foreach 循环,自定义幻灯片
【发布时间】:2017-07-06 07:03:17
【问题描述】:

它应该是一个滑块,显示所有帖子,但每张幻灯片上只有 15 个帖子。我得到所有的帖子。

(我使用 Wordpress 功能。)

在 -div class= "slide"- 中有 15 个帖子,之后应该创建一个新的 -div class= "slide"- 有 15 个帖子。

这里是所有帖子的代码:

$myposts = get_posts($args);




$result = '<div id="fullpage">';
    $result .= '<div class="section" id="section1">';

        $result .= '<div class="slide">';
            foreach ($myposts as $post) {
                $result .= '<a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a>';
                 //        the_post_thumbnail('full');
            }

        $result .= '</div>';
    $result .= ' </div>';

$result .= '</div>';


return $result;

在 15 个帖子之后,我想要一张新幻灯片。我不知道如何调整 foreach 循环。我应该使用 if 语句执行此操作,还是可以使用 foreach 循环执行此操作?

【问题讨论】:

  • 您可以在 foreach 和 if 条件中添加计数器来检查计数器值 15

标签: php wordpress foreach


【解决方案1】:

使用计数器,15后重置。(未测试,我目前没有PHP)

$counter = 1;
foreach ($myposts as $post) {
    if ($counter == 1) {
        $result .= '<div class="slide">';
    }
    $result .= '<a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a>';
    $counter++;
    if ($counter == 16) {
        $counter = 1;
        $result .= '</div>';
    }
}

【讨论】:

  • 感谢您的快速帮助! :) 正是我需要的。
猜你喜欢
  • 2013-04-04
  • 2013-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 2012-09-19
  • 2012-08-11
  • 2014-01-18
相关资源
最近更新 更多