我有CPT叫“天赐”(英文:开放时间)。我有7个帖子(每个工作日: mon,tue..sunday)。这些帖子的内容是开放时间,比如:09.00-21.00。我想达到的目标是有一个循环,它将显示今天的开放时间。
所以我现在的循环是:
<?php
$args = array(
'post_type' => 'godziny-otwarcia',
'post__in' => array(1, 2, 3, 4, 5, 6, 7)
);
$query = new WP_Query($args);
while ($query->have_posts()) : $query->the_post();
?>
<div class="single-row">
<div class="single-day">
<span class="day-desc"><?php the_title(); ?></span>
<span class="timing"><?php $opening_hours = types_render_field("opening-hours", array("raw"=>"true","separator"=>";")); echo $opening_hours; ?> <i class="fa fa-clock-o"></i>
</span>
</div>
</div> 然后我知道我应该做7种不同的if声明来检查工作日:
if ( date ( 'w' ) == 1 ) {
the_content();
}
else if( date ( 'w' ) == 2 ) {
the_content();
}
else if( date ( 'w' ) == 3 ) {
the_content();
}
else if( date ( 'w' ) == 4 ) {
the_content();
}
else if( date ( 'w' ) == 5 ) {
the_content();
}
else if( date ( 'w' ) == 6 ) {
the_content();
}
else if( date ( 'w' ) == 0 ) {
the_content();
}但不幸的是--我不知道如何将语句与循环连接起来;/它甚至可行吗?谢谢你所有的建议/建议。
发布于 2017-10-13 08:01:00
你可以试试这个:
if ( date ( 'w' ) == 1 ) {
$monday = new WP_Query('post_type=godziny-otwarcia&p=1');
echo $monday->the_content;
}
else if( date ( 'w' ) == 2 ) {
$tuesday = new WP_Query('post_type=godziny-otwarcia&p=2');
echo $tuesday->the_content;
}
else if( date ( 'w' ) == 3 ) {
etc.
}
else if( date ( 'w' ) == 4 ) {
etc.
}
else if( date ( 'w' ) == 5 ) {
etc.
}
else if( date ( 'w' ) == 6 ) {
etc.
}
else if( date ( 'w' ) == 0 ) {
etc.
}你不需要把这个循环起来。
https://stackoverflow.com/questions/46724082
复制相似问题