我为WordPress设置了一个循环,它从自定义字段(日期选择器)获取日期并将其传递给数组。 我需要确定该阵列中未来最近的日期(即将到来的日期)。
<?php $today = current_time('m/d/Y'); $args=array( 'meta_key' => 'opening', 'orderby' => 'meta_value', 'order' => 'ASC', 'posts_per_page' => -1, 'post_type' => 'event', ); $query = new WP_Query($args); ?> <?php if($query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?> <?php $date = DateTime::createFromFormat('Ymd', get_field('opening')); $dates[] = $date->format('m/d/Y'); endwhile; endif; wp_reset_postdata(); print_r($dates); ?>并将其传递给javascript。
$('#tl').timeline({ startItem : '$resultOfUpcomingDateEvaluation', });请指教,谢谢你的时间。
I have setup a loop for WordPress which gets the date from a custom field (date picker) and passes it to a array. I need to determine the closest date in the future (upcoming date) from that array.
<?php $today = current_time('m/d/Y'); $args=array( 'meta_key' => 'opening', 'orderby' => 'meta_value', 'order' => 'ASC', 'posts_per_page' => -1, 'post_type' => 'event', ); $query = new WP_Query($args); ?> <?php if($query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?> <?php $date = DateTime::createFromFormat('Ymd', get_field('opening')); $dates[] = $date->format('m/d/Y'); endwhile; endif; wp_reset_postdata(); print_r($dates); ?>And pass it to javascript.
$('#tl').timeline({ startItem : '$resultOfUpcomingDateEvaluation', });Please advice, thanks for the time.
最满意答案
找到一个解决方案,有点脏,如果有人有一个更优雅的一个将是很好的。
我用类似的设置写了第二个循环,将帖子限制为1并将meta_value与获得current_time的var进行比较。 然后我回应结果将它包装在div中,并使用隐藏它的类(不优雅)。
<?php $today = current_time('Ymd'); $args=array( 'meta_key' => 'opening', 'orderby' => 'meta_value', 'order' => 'ASC', 'posts_per_page' => 1, 'post_type' => 'event', 'meta_query' => array( 'key' => 'opening', 'compare' => '>=', 'value' => $today, ) ); $query = new WP_Query($args); ?> <?php if($query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?> <?php $date = DateTime::createFromFormat('Ymd', get_field('opening')); $upcoming = $date->format('d/m/Y'); endwhile; endif; wp_reset_postdata(); echo '<div class="hide" id="upcomingdiv">'; echo $upcoming; echo '</div>'; ?>之后,在我的javascript文件中,我通过ID获取元素并获取包含循环结果的div的.textContent并将其存储到变量中。
var setupUpcoming = document.getElementById("upcomingRef"); var upcoming = setupUpcoming.textContent;最后我将变量传递给startItem。
$('#tl').timeline({ startItem : upcoming, });Found a solution, little dirty so will be nice if someone has a more elegant one.
I wrote a second loop with a similar setup, limiting the posts to 1 and comparing the meta_value with a var that gets current_time. Then i echo the result wrapping it in a div with a class that hides it (not elegant).
<?php $today = current_time('Ymd'); $args=array( 'meta_key' => 'opening', 'orderby' => 'meta_value', 'order' => 'ASC', 'posts_per_page' => 1, 'post_type' => 'event', 'meta_query' => array( 'key' => 'opening', 'compare' => '>=', 'value' => $today, ) ); $query = new WP_Query($args); ?> <?php if($query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?> <?php $date = DateTime::createFromFormat('Ymd', get_field('opening')); $upcoming = $date->format('d/m/Y'); endwhile; endif; wp_reset_postdata(); echo '<div class="hide" id="upcomingdiv">'; echo $upcoming; echo '</div>'; ?>After that, on my javascript file, i get the element by ID and get the .textContent of the div that contains the result of the loop and store it to a variable.
var setupUpcoming = document.getElementById("upcomingRef"); var upcoming = setupUpcoming.textContent;Finally i pass the variable to the startItem.
$('#tl').timeline({ startItem : upcoming, });更多推荐
发布评论