简体   繁体   English

循环的第1次和第3次迭代

[英]Every 1st and 3rd Iteration of a Loop

I'm making an update to an old client WP site (hence the 960.gs grid) and have a loop to output a lost of news items. 我正在更新旧的客户端WP网站(因此为960.gs网格),并且有一个循环来输出丢失的新闻项目。

What I am trying to achieve (with an if statement) is to set the .alpha class to the 1st item as well as numbers 4, 7, 10, 13... etc. I am also trying to apply .omega to 3, 6, 9, 12... etc 我想要实现的(使用if语句)是将.alpha类设置为第一项以及数字.omega ...等。我还尝试将.omega应用于3, 6,9,12 ...等

Here is my if statement with modulus operators: <?php if ($count % 1 == 0) : ?>alpha <?php endif; ?>grid_4<?php if ($count % 3 == 0) : ?> omega<?php endif; ?> 这是我的带模运算符的if语句: <?php if ($count % 1 == 0) : ?>alpha <?php endif; ?>grid_4<?php if ($count % 3 == 0) : ?> omega<?php endif; ?> <?php if ($count % 1 == 0) : ?>alpha <?php endif; ?>grid_4<?php if ($count % 3 == 0) : ?> omega<?php endif; ?>

But this is giving me the following: 但这给了我以下几点:

<div class="feed">

    <div class="alpha grid_4">

        <p>In condimentum facilisis porta. Sed nec diam eu diam mattis viverra. Nulla fringilla, orci ac euismod semper, magna diam porttitor mauris, quis sollicitudin.</p>

    </div>

    <div class="alpha grid_4">

        <p>Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Proin vel ante a orci tempus eleifend ut et magna. Lorem ipsum dolor sit amet, consectetur adipiscing […]</p>

    </div>  

    <div class="alpha grid_4 omega">

        <p>Suspendisse dictum feugiat nisl ut dapibus. Mauris iaculis porttitor posuere. Praesent id metus massa, ut blandit odio. Proin quis tortor orci. Etiam at risus et justo dignissim congue. Donec congue […]</p>

    </div>

    <div class="alpha grid_4">

        <p>Duis aliquet egestas purus in blandit. Curabitur vulputate, ligula lacinia scelerisque tempor, lacus lacus ornare ante, ac egestas est urna sit amet arcu. Class aptent taciti sociosqu ad litora torquent […]</p>

    </div>

    <div class="alpha grid_4">

        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus luctus urna sed urna ultricies ac tempor dui sagittis. In condimentum facilisis porta. Sed nec diam eu diam mattis viverra. Nulla […]</p>

    </div>

    <div class="alpha grid_4 omega">

        <p>Curabitur vulputate, ligula lacinia scelerisque tempor, lacus lacus ornare ante, ac egestas est urna sit amet arcu. Class aptent taciti sociosqu ad litora torquent per conubia. Curab itur vulputate, ligula […]</p>

    </div>

    <div class="alpha grid_4">

        <p>Mauris iaculis porttitor posuere. Praesent id metus massa, ut blandit odio. Proin quis tortor orci. Etiam at risus et justo dignissim congue. Donec congue lacinia dui, a porttitor lectus condimentum […]</p>

    </div>

</div>

As you can see, .omega appears to be working correctly, but .alpha is getting applied to every element. 如您所见, .omega似乎工作正常,但是.alpha已应用于每个元素。

You are dividing by 1, so alpha is applied everywhere. 您除以1,所以alpha应用于所有位置。

Change if statement for alpha like this: 更改alpha的if语句,如下所示:

<?php if ($count % 3 == 1) : ?>alpha <?php endif; ?>grid_4<?php if ($count % 3 == 0) : ?> omega<?php endif; ?>

A solution might be : 一个解决方案可能是:

<?php if ($count == 0 ) : ?>alpha <?php endif; ?>grid_4<?php if ($count % 3 == 0) : ?> omega<?php endif; ?>

In a nutshell, the modulus (%) operation with a 1 operand will return 0 on any number, since any integer can be divided by 1 (result being the number itself, so the modulus is zero). 简而言之,操作数为1的模数(%)运算将对任何数字返回0,因为任何整数都可以除以1(结果是数字本身,所以模数为零)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM