dpg78570 2013-08-14 20:27
浏览 9
已采纳

使用PHP改变每1/5条目的类

Using PHP to generate out a list.

I Simply want to set a different class on every fifth entry. (1/5), (4+1)

<li class="<?php the_code();?>"> content </li>

Output would be like this

<li class="yolo"> Content </li>
<li class="yolo"> Content </li>
<li class="yolo"> Content </li>
<li class="yolo"> Content </li>
<li class="hello"> Content </li>
<li class="yolo"> Content </li>
<li class="yolo"> Content </li>
<li class="yolo"> Content </li>
<li class="yolo"> Content </li>
<li class="hello"> Content </li>

How can i do this ?

Guess it's pure basic for anyone with php skills, that's not me but i would also be glad to get pointed towards some page that explains this so i can learn it.

To clarify am using Wordpress loop and it contains +100 items and i want to alter class on item 5,10,15,20,25,30,35 etc..

  • 写回答

2条回答 默认 最新

  • dougou8458 2013-08-14 20:31
    关注

    You can use a counter ($i) and increment it on each time you create a new list item (++$i), then check to see if the result is divisible by 5 (% 5 == 0). Then simply use the ternary operator (?…:…) to decide which value to output:

    <?php $i = 0; ?>
    
    <?php for(...) { ?>
      <li class="<?= (++$i) % 5 == 0 ? 'yolo' : 'hello' ?>"> content </li>
    <?php } ?>
    

    But it truth, there's no need to even use PHP, when a little CSS will probably do the job:

    li:nth-child(5n)
    {
        ...
    }
    

    See a demonstation here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名