2017-10-13 120 views
0

我有一个叫做godziny-otwarcia的CPT(英文:开放时间)。我有7个职位(每周一个工作日:星期一,星期五)。这些帖子的内容是开放时间如:09.00 - 21.00。我想实现的目标是建立一个循环,以显示今天的开放时间。WP自定义循环 - 根据工作日显示特定的CPT帖子

所以我目前的循环是:

<?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(); 
} 

但unfortuntelly - 我不知道如何连接声明,循环; /它甚至是可行的?感谢您所有的提示/建议..

+0

诚实地说 - 这似乎是你在这里重新发明深盘。 - 当我创建开放时间时,我通常会使用此插件 - https://github.com/janizde/WP-Opening-Hours - 支持一直不错,文档很棒 - 并且可以像你一样播放短代码或小部件想。 – Stender

+0

谢谢,但如果仍然我想用我自己的解决方案?我试图尽可能限制插件的使用 –

回答

1

你可以试试这个:

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. 
} 

你不需要把这个循环。

相关问题