【问题标题】:Show or hide content based on ACF time/date field根据 ACF 时间/日期字段显示或隐藏内容
【发布时间】:2020-07-17 22:28:26
【问题描述】:

如何使用两个 ACF 日期/时间字段在特定日期的特定时间显示和隐藏内容?我正在主持现场视频活动。我需要在现场活动开始之前显示占位符视频,然后在开始和结束日期/时间(ACF 日期/时间字段)之间显示现场视频。我需要在活动结束日期/时间之后再次显示占位符视频。

我已经为 ACF 配置了一个开始日期/时间字段和一个结束日期/时间字段。

`<?php
$current_date = date('Y-m-d H:i:s');
$current_time = strtotime($current_date);
$start = get_field('start_date');
$end = get_field('end_date');
if ($current_time >= $start && $current_time <= $end){ 
//show content if between start and end time
echo '<h3>We are Live</h3>';
} else {
//show only before or after start & end time
echo '<h6>Not Live</h6>';
}
?>`

似乎只考虑一天而不考虑时间。

【问题讨论】:

    标签: php wordpress datetime advanced-custom-fields acfpro


    【解决方案1】:

    我会尝试使用 WP_Query 并使用带有 'relation' 的 meta_query 数组

    $query = new WP_Query ( array (
    meta_query => array(
      'relation' => 'AND',
    array (
      'key' => 'start_date',
      'compare' => '>=',
      'value'.  => $current_time,
      'type' => 'TIME',
    ),
     array (
      'key' => 'end_date',
      'compare' => '<=',
      'value'.  => $current_time,
      'type' => 'TIME',
    )
    )
    ));
     if ( $query->have_posts() ) { while( $query->have_posts() ) {
    $query->the_post();
    // Your Stuff Here
    }
    }
    

    【讨论】:

      【解决方案2】:

      我能够通过将开始和结束时间转换为 UnixTime 并添加一个字段来增加或减少时区差异的小时数来实现这一点。

      <?php
      $current_date = date('Y-m-d H:i:s');
      $current_time = strtotime($current_date);
      $time_zone = get_field('time_zone');
      $now = strtotime( $time_zone['value'], $current_time);
      $start = strtotime(get_field('start_date'));
      $end = strtotime(get_field('end_date'));                
      
      if ($now >= $start && $now <= $end){
        // Show  live video between start & end time
      } else {
        // Show placeholder video before and after start & end time
      }
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-13
        • 2011-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-07
        • 2019-10-01
        • 1970-01-01
        相关资源
        最近更新 更多