wordpress甚至奇怪的循环

时间:2014-04-09 17:54:40

标签: php wordpress

我正在尝试在Wordpress中实现一个具有奇数/偶数模式的循环。这不起作用。它循环每个帖子10次。如何在原始foreach循环中集成计数循环?

<?php  
$i=0;
$posts = get_posts(array('posts_per_page'=>9,'offset'=> 6));
foreach( $posts as $post ) : setup_postdata($post);

for ($i = 0; $i < 10; $i++)
{
  if ($i % 2 == 0)
  { ?>

我的奇怪内容

<?php  }
    else
    {
 ?>

我的内容

<?php  }
    }
?>



<?php
$i++;
endforeach;
wp_reset_postdata();
?>

1 个答案:

答案 0 :(得分:0)

for($i = 0; $i < 10; $i++){
    if($i % 2 == 0){
        //echo even
        echo "Even: " . $i . "<br/>";
    } else {
        //echo odd
        echo "Odd: " . $i . "<br/>";
    }
}

简单的循环

您的内容:

foreach( $posts as $post ) : setup_postdata($post);
   if($i % 2 == 0){
            //echo even
            echo "Even: " . $i . "<br/>";
        } else {
            //echo odd
            echo "Odd: " . $i . "<br/>";
        }
$i++
endforeach;

完整代码:

<?php  
$i=0;
$posts = get_posts(array('posts_per_page'=>9,'offset'=> 6));
foreach( $posts as $post ) : setup_postdata($post);
  if($i % 2 == 0){
        //echo even
        echo "Even: " . $i . "<br/>";
    } else {
        //echo odd
        echo "Odd: " . $i . "<br/>";
    }
$i++;
endforeach;
wp_reset_postdata();
?>