div 的高度基于高级自定义字段日期条目



我正在使用高级自定义字段在Wordpress中构建一个时间线页面。我有一个活动的开始日期和结束日期,我需要根据事件的持续时间调整div 的高度。

到目前为止,我拥有的代码如下。 <div class="span"></div>是需要高度的div。我需要的高度比估计约为每 100 天 30.5px。开始/结束日期的字段名称为 timeline_datespan_starttimeline_datespan_end,字段类型为日期选取器。

<?php 
  $custom_args = array(
      'post_type'       => 'timeline',
      'post_status'     => 'publish',
      'posts_per_page'  => -1,
      'meta_key'        => 'timeline_date',
      'orderby'         => 'meta_value_num',
      'order'           => 'ASC'
    );
  $custom_query = new WP_Query( $custom_args ); ?>
<?php if ( $custom_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
    <!-- event span -->
    <?php 
        $timeline_span = get_field('timeline_datespan_start');
        if( !empty($timeline_span) ): ?>
        <div class="event <?php the_field('timeline_datespan_start'); ?>">
            <div class="timeline-event-span">
                <div class="span"></div><div class="line"></div>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <span class="timeline-date">(<?php the_field('timeline_datespan_start'); ?> - <?php the_field('timeline_datespan_end'); ?>)</span>
            </div>
        </div>
    <?php endif; ?>
    <!-- /event span -->
    <?php // endif; ?>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else:  ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

试试这个:

  $startDate = the_field('timeline_datespan_start');
  $endDate   = the_field('timeline_datespan_end');
  $datetime1 = date_create($startDate);
  $datetime2 = date_create($endDate);
  $dDiff = $datetime1->diff($datetime2);
  $height = ($dDiff->days) / 0.305;

非常感谢您为我指明正确的方向。以下是它为我工作的原因:

<?php 
    $startdate = get_field('timeline_datespan_start', false, false);
    $enddate = get_field('timeline_datespan_end', false, false);
    $startdate = new DateTime($startdate);
    $enddate = new DateTime($enddate);
    $dDiff = $startdate->diff($enddate);
    $height = ($dDiff->days) / 0.305;
?>

最新更新