【问题标题】:Add CSS code to every 3rd iteration of loop - WordPress将 CSS 代码添加到循环的每 3 次迭代 - WordPress
【发布时间】:2011-11-20 21:21:17
【问题描述】:

我想为这个 WP 循环的每 3 次迭代添加一个 style="background:green"

我如何做到这一点?

   if( have_posts() ) :
    while ($wp_query->have_posts()) : $wp_query->the_post();
    ?>
    <li>Test</li>
    <?php endwhile; ?>
    <?php endif;

非常感谢您的任何指点。

【问题讨论】:

    标签: html css wordpress loops while-loop


    【解决方案1】:

    您是否尝试过使用 % 运算符。类似于以下内容(未经测试):

    if( have_posts() ) :
    $i=0;
    while ($wp_query->have_posts()) : $wp_query->the_post();
    $i++;
    ?>
    <li <?php if(($i % 3)==0)echo 'style="background:green"';?>>Test</li>
    <?php endwhile; ?>
    <?php endif;
    

    PHP 参考:http://php.net/manual/en/language.operators.arithmetic.php

    【讨论】:

      【解决方案2】:

      也许是一个递增变量和模运算符的使用,只是一个想法。 http://php.net/manual/en/language.operators.arithmetic.php,与 WP 类似:http://www.ilovecolors.com.ar/ads-wordpress-loop/

      【讨论】:

        猜你喜欢
        • 2014-07-06
        • 2017-09-05
        • 1970-01-01
        • 2018-05-23
        • 2018-03-15
        • 2017-10-04
        • 2019-11-28
        • 1970-01-01
        • 2015-03-19
        相关资源
        最近更新 更多