WordPress循环,将类添加到第二个对象

时间:2015-06-08 12:18:09

标签: php wordpress loops

我有以下代码:

<?php 
    $args = array(
        'category__in' => array(3),
        'posts_per_page' => 3
    );
    $loop = new WP_Query( $args );
    while ($loop->have_posts() ) : $loop->the_post();
?>

<div class="col-md-4 col-sm-4 col-xs-12" style="padding-left:0; padding-right:0;">
    <a style="text-decoration:none" href="<?php echo get_permalink(); ?>">
        <div class="postsize" style="background:#242930; color:white !important;">
            <?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>
            <div class="contentfaq" style="padding: 0 20px 20px; min-height:235px;">
                <h3 style="color:white !important;"><?php the_title(); ?></h3>

                <div style="color:white";>
                    <p><?php echo content2(15); ?></p>
                </div>
            </div>
        </div>
    </a>
</div>

<?php endwhile; ?>
<?php wp_reset_query(); ?> 

获取我的最新帖子,并以块状显示。

现在,我正在尝试将第二个块背景设为白色,但不能将其作为目标,我不确定如何使用。

有快速定位方法吗?

1 个答案:

答案 0 :(得分:2)

创建一个变量并检查你是否处于循环的第二步。

$i = 1;
while ($loop->have_posts() ) : $loop->the_post();
  ?>
    <div class="col-md-4 col-sm-4 col-xs-12" style="padding-left:0; padding-right:0;">    
        <a style="text-decoration:none; color:white;" href="<?php echo get_permalink(); ?>">
        <div class="postsize" style="<?php echo $i == 2 ? 'background: white; color: black;' : 'background: #242930; color: #fff !important'; ?>">...
    ...
<?php
    $i++;
endwhile;
?>
相关问题