对每一个第n项(每一个都是奇数,甚至是) [英] Foreach, special treatment of every n:th item (odd, even for example)

查看:158
本文介绍了对每一个第n项(每一个都是奇数,甚至是)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  foreach($ blogusers as $ bloguser){
$ args = array(
'author'=> $ bloguser-> user_id,
'showposts'=> 1,
'caller_get_posts'=> 1
);
$ my_query = new WP_Query($ args);
if($ my_query-> have_posts()){
$ user = get_userdata($ bloguser-> user_id);
userphoto($ bloguser-> user_id,< div class ='all_authors'>< a href ='http://blogg.nacka.se/nastasteg/author/。$ user-> ; user_login。'>,< / a>< ul>< li>< a href ='http://blogg.nacka.se/nastasteg/author/。 user_login。'>。$ user-> user_firstname。< / a>< / li>< li class ='occupation'> ;. $ user-> user_lastname。< / li> ;< / ul>< / div>,array('width'=> 135,'height'=> 135));
#echo< div class ='all_authors'>< a href ='http://blogg.nacka.se/nastasteg/author/。$ user-> user_login。 >< img src ='http://www.gravatar.com/avatar/。 md5(strtolower(trim($ user-> user_email)))。?s = 135'/>< / a>< ul>< li>< a href ='http:// blogg.nacka.se/nastasteg/author/\".$user-> user_login。 >中。$ user-> user_firstname。< / a>< / li>< li class ='occupation'> ;. $ user-> user_lastname。< / li>< / ul>< ; / DIV>中;






$ b在每一个f div中,想增加一个额外的类。我该怎么做呢?

解决方案

使用模数运算符。我看到@Alex已经把我打败了,但是我提供了我编写和测试的代码,以便其他人可以更清楚地看到原理:

  $ blogusers =阵列( 'A', 'b', 'C', 'd', 'E', 'F', 'G', 'H', 'I','Ĵ ); 
$ i = 0;
foreach($ blogusers as $ bloguser){
if($ i%4 === 0)$ extraclass =fourthClass;
$ resultHTML。=< div class = \standardClass $ extraclass \> $ bloguser< / div>;
$ i ++;
$ extraclass =;
}
echo $ resultHTML;

可以使用三元运算符更紧凑,但这是原则。 b $ b

I have a foreach that looks like this:

                          foreach ($blogusers as $bloguser) {
                        $args = array(
                        'author' => $bloguser->user_id,
                          'showposts' => 1,
                          'caller_get_posts' => 1
                        );
                        $my_query = new WP_Query($args);
                        if( $my_query->have_posts() ) {
                          $user = get_userdata($bloguser->user_id);
                          userphoto($bloguser->user_id, "<div class='all_authors'><a href='http://blogg.nacka.se/nastasteg/author/".$user->user_login . "'>","</a><ul><li><a href='http://blogg.nacka.se/nastasteg/author/".$user->user_login . "'>" .$user->user_firstname."</a></li><li class='occupation'>".$user->user_lastname."</li></ul></div>", array('width' => 135, 'height' => 135));
                          #echo "<div class='all_authors'><a href='http://blogg.nacka.se/nastasteg/author/".$user->user_login . "'><img src='http://www.gravatar.com/avatar/" . md5( strtolower( trim( " $user->user_email " ) ) )."?s=135' /></a><ul><li><a href='http://blogg.nacka.se/nastasteg/author/".$user->user_login . "'>" .$user->user_firstname."</a></li><li class='occupation'>".$user->user_lastname."</li></ul></div>";
                        }
                      }

In every foth div, Would like to add an extra class. How do I do this?

解决方案

Use the modulus operator. I see @Alex has beaten me to the mark with this, but I offer this code I wrote and tested, so that others can see more clearly the principle:

$blogusers=array('a','b','c','d','e','f','g','h','i','j');
$i=0;
foreach ($blogusers as $bloguser) {
    if($i % 4 === 0) $extraclass= "fourthClass";
    $resultHTML .= "<div class=\"standardClass $extraclass\">$bloguser</div>";
    $i++;
    $extraclass="";
}
echo $resultHTML;

Could be made more compact with the ternary operator, but this is the principle.

这篇关于对每一个第n项(每一个都是奇数,甚至是)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆