我正试图让我的反击,好吧,数数。我正在尝试使用以下内容为显示的每个帖子添加一个类名(偶数):
<?php
global $paged;
global $post;
$do_not_duplicate = array();
$categories = get_the_category();
$category = $categories[0];
$cat_ID = $category->cat_ID;
$myposts = get_posts('category='.$cat_ID.'&paged='.$paged);
$do_not_duplicate[] = $post->ID;
$c = 0;
$c++;
if( $c == 2) {
$style = 'even animated fadeIn';
$c = 0;
}
else $style='animated fadeIn';
?>
<?php foreach($myposts as $post) :?>
// Posts outputted here
<?php endforeach; ?>
我只是没有输出even
类名。输出的唯一类名是animated
和FadeIn
类(来自我的if语句的else部分),目前已添加到每个帖子中
答案 0 :(得分:1)
另外,将偶数/奇数检查移至帖子循环。
<?php $i = 0; foreach($myposts as $post) :?>
<div class="<?php echo $i % 2 ? 'even' : 'odd'; ?>">
// Posts outputted here
</div>
<?php $i++; endforeach; ?>
答案 1 :(得分:0)
从代码的这一部分开始:
$c = 0;
$c++;
if( $c == 2) {
$style = 'even animated fadeIn';
$c = 0;
}
else $style='animated fadeIn';
您需要将增量和if-else
块放在foreach
循环中。像这样:
<?php foreach($myposts as $post) :
$c++;
if( $c % 2 == 0) {
$style = 'even animated fadeIn';
$c = 0;
}
else $style='animated fadeIn'; ?>
// Posts outputted here
<?php endforeach; ?>
答案 2 :(得分:0)
问题是你不要在你的else语句中将计数器设置回2.
if( $c == 2) {
$style = 'even animated fadeIn';
$c = 0;
} else {
$style='animated fadeIn';
$c = 2;
}
话虽如此,您也可以像其他人所提到的那样使用模数,或者只是这样做:
//outside loop
$c = 1;
//inside loop
if ($c==1)
$style = 'even animated fadeIn';
else
$style='animated fadeIn';
$c = $c * -1;
甚至更短
//outside
$c = 1;
//inside
$style = (($c==1)?'even':'').' animated fadeIn';
$c = $c * -1;