dongzhanlu0658 2017-10-13 06:52
浏览 54
已采纳

WP自定义循环 - 根据工作日显示特定的CPT帖子

I'have CPT called godziny-otwarcia (in english: opening hours). I've got there 7 posts (each for one weekday: mon, tue..sunday). The content of this posts are opening hours like: 09.00 - 21.00. What I would like to achieve is to have a loop which will show opening hours for today.

So my current loop is:

<?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>    

Then I know that I should make 7 different if statements to check the weekday:

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

But unfortuntelly - I have no clue how to connect the statement, with the loop ;/ is it even doable? Thank you for all the tips/suggestions..

  • 写回答

1条回答 默认 最新

  • dseslyh6662605 2017-10-13 08:01
    关注

    You can try this:

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

    You don't need to put this in a loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成