twitter-bootstrap 在(foreach)循环中向第一个div添加一个类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20940057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a class to first div in (foreach) loop
提问by Jeroen Balk
I'm using the carousel that comes with bootstrap. This I'll be using in WordPress. I'm querying two recent posts with a foreach loop but for the carousel to work correctly I need the newest post to have an extra 'active' class. I found some solutions here on stackoverflow but it were all whileloops, I really need it for this foreach loop. This is my code:
我正在使用引导程序附带的轮播。我将在 WordPress 中使用它。我正在使用 foreach 循环查询最近的两篇帖子,但为了使轮播正常工作,我需要最新的帖子来拥有一个额外的“活动”类。我在stackoverflow上找到了一些解决方案,但都是while循环,我真的需要它来处理这个foreach循环。这是我的代码:
<div id="NewsCarousel" class="carousel slide">
<div class="carousel-inner">
<?php
$args = array( 'numberposts' => '2', 'category' => 5 );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<div class="item"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], array(200,200)) .$recent["post_title"].'</a> </div> ';
}
?>
</div>
</div>
回答by Mike Vranckx
You can use a booleanvariable to determine if it is a first loop or not. Initial value is true, once it loops, the value is set to false.
您可以使用boolean变量来确定它是否是第一个循环。初始值为true,一旦循环,该值将设置为false。
<div id="NewsCarousel" class="carousel slide">
<div class="carousel-inner">
<?php
$args = array( 'numberposts' => '2', 'category' => 5 );
$recent_posts = wp_get_recent_posts( $args );
$isFirst = true;
foreach( $recent_posts as $recent ){
echo '<div class="item' . $isFirst ? ' active' : '' . '"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], array(200,200)) .$recent["post_title"].'</a> </div> ';
$isFirst = false;
}
?>
</div>
</div>
回答by Jason Bassett
You can add a counter like so $count = 0;outside of the foreach loop. And then inside the foreach loop you tell it to increment like so $count++;
您可以$count = 0;在 foreach 循环之外添加一个像这样的计数器。然后在 foreach 循环中告诉它像这样递增$count++;
You then check if the count is equal to 1 like this: if($count == 1){//do this}
然后,您可以像这样检查计数是否等于 1: if($count == 1){//do this}
So in your case lets do it like this:
所以在你的情况下,让我们这样做:
<div id="NewsCarousel" class="carousel slide">
<div class="carousel-inner">
<?php
$args = array( 'numberposts' => '2', 'category' => 5 );
$recent_posts = wp_get_recent_posts( $args );
$count = 0;
foreach( $recent_posts as $recent ){
$count++;
echo '<div class="item'; if($count == 1){echo ' active';}"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], array(200,200)) .$recent["post_title"].'</a> </div> ';
}
?>
</div>
</div>
Try that, it should do the trick. I just used this method on a project of which I'm dealing with currently.
试试看,它应该可以解决问题。我刚刚在我目前正在处理的项目中使用了这种方法。

