简体   繁体   English

有条件的CSS类,取决于日期的年龄

[英]Conditional CSS Class dependent on age of date

I'm trying to create a situation where I have an unordered list (created using Wordpress plugin Advanced Custom Fields) and within it, all list items that have a date older that 30 days have a different css class (so I can style them differently). 我正在尝试创建一种情况,即我有一个无序列表(使用Wordpress插件“高级自定义字段”创建),并且其中所有日期早于30天的列表项都具有不同的CSS类(因此我可以使用不同的样式来设置它们) )。 With this in mind, can anyone show me how to edit the code below so that when the_sub_field(date) in the 2nd div on each li is older than 30 days it will change the <li class='new'> to <li class='old'> 考虑到这一点,任何人都可以告诉我如何编辑以下代码,以便当每个li的第二个div中的the_sub_field(date)大于30天时,它将<li class='new'>更改为<li class='old'>

See code below: 参见下面的代码:

        // check for rows
        <?php if( have_rows('item_list_details') ): ?>
            <ul class="admin">
            <?php
            // loop through rows
            while( have_rows('item_list_details') ): the_row() 
                // display each item as a list
                ?>
                   <li class='new'>
                <div class='itemTitle'>
                    <?php the_sub_field('link_name'); ?>
                </div>
                <div class="memberListDate">
                          <?php the_sub_field('date'); ?>
                     </div>
                   </li>   
            <?php endwhile; ?>
            </ul>
        <?php endif; ?>

Any help with this would be most appreciated 任何对此的帮助将不胜感激

Replace 更换

<li class='new'>

With

<?php
    $date = new DateTime(the_sub_field('date'));
    $now = new DateTime(Date('Y-m-d'));
    $diff = $now->diff($date);

    if ($diff->days > 30):    //Use $diff->days and not $diff->d
?>
    <li class='old'>
<?php else: ?>
    <li class='new'>
<?php endif;?>

Contents of $diff $ diff的内容

object(DateInterval)#3 (15) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(0)
  ["d"]=>
  int(18)                        <-------d is relative to a single month
  ["h"]=>
  int(0)
  ["i"]=>
  int(0)
  ["s"]=>
  int(0)
  ["weekday"]=>
  int(0)
  ["weekday_behavior"]=>
  int(0)
  ["first_last_day_of"]=>
  int(0)
  ["invert"]=>
  int(1)
  ["days"]=>
  int(18)                        <--------days is the total days between
  ["special_type"]=>
  int(0)
  ["special_amount"]=>
  int(0)
  ["have_weekday_relative"]=>
  int(0)
  ["have_special_relative"]=>
  int(0)
}

Original Code with var_dump 原始代码与var_dump

` "; var_dump(get_sub_field('age')); echo ""; ?> diff($date); `“; var_dump(get_sub_field('age')); echo”“;?> diff($ date);

                                if ($diff->days > 30):
                            ?>
                                <li class='research'>
                            <?php else: ?>
                                <li class='researchLatest'>
                            <?php endif;?>`

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

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