我想动态地实现以下html:
时间段AD:
1301
时间段BC:
1300
在下面我设置了一个数组,以便我可以通过使用一些自定义字段将我的日期推入数组,然后告诉整个代码在100年内计算,以便能够像 <ul>
<?php
$yearsArray = [];
$centuryHash = [];
query_posts(array(
'post_type' => 'post',
'posts_per_page' => -1
));
while ( have_posts() ) : the_post();
array_push($yearsArray, get_field("year"));
if (($wp_query->current_post +1) == ($wp_query->post_count)) {
$yearsArray = array_unique($yearsArray);
sort($yearsArray);
}
endwhile;
foreach ($yearsArray as $year) {
$currentCentury = floor($year/100)*100;
if(!$centuryHash[$currentCentury]){
$centuryHash[$currentCentury] = [];
}
if($currentCentury != $year){
$centuryHash[$currentCentury][] = $year;
}
}
foreach ($centuryHash as $century => $centuryYears) { ?>
<li class="dropdown">
<?php if($centuryYears){ ?>
<a class="btn btn-default" href="#" class="dropdown-toggle" data-date="<?php echo $century; ?>" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<?php echo $century; ?>
<span class='caret'></span>
</a>
<ul class='dropdown-menu'>
<?php foreach ($centuryYears as $year) { ?>
<li>
<a class="btn btn-default" data-date="<?php echo $year; ?>" href="#">
<?php echo $year; ?>
</a>
</li>
<?php }
echo "</ul>";
} else { ?>
<a class="btn btn-default" data-date="<?php echo $century; ?>" href="#">
<?php echo $century; ?>
</a>
<?php } ?>
</li>
<?php }
?>
</ul>
作为if
下的嵌套ul。
foreach (range(0, 12) as $number) {
echo $number;
}
我无法弄清楚的问题是我可以这样说(口头代码):
如果值在-450到2的范围内,则进入此数组,否则进入该数组
从我的结尾我可以设置cms(人们将能够插入带日期的内容)一些标志,这样我就可以做简单的条件,如(口头代码):
&#34;这个日期来自a还是b?然后做这个或那个&#34;
但这会产生一个问题,因为我可以标记一段时间并在错误的时间内输入错误的日期。
因此我认为最后一个解决方案是设置一系列时间段并通过创建不同的数组来定界某些数组,我可以根据它们的值动态推送值。
这可能是一个开始(example from docs)?但是如何设置{{1}}来控制那里是什么?
{{1}}
答案 0 :(得分:2)
以下是您问题的简单答案。
如果值在-450到2的范围内,则进入此数组,否则进入该数组
if ($value >= -450 && $value <= 2) {
// do this
} else {
// do that
}
希望,这可以回答你的问题(不幸的是,这个问题超过了几十个不适用于该问题的代码)。