我根据“event_date”的自定义字段按周显示事件(自定义帖子类型)。我有初始负载工作。它的作用是获取当前周的开始和结束,并返回那段时间内的事件。我需要做的是单击下一个或上一个按钮并根据该周更新事件(事件应通过Ajax更新)。
这是我的jQuery点击功能。它只是点击'get-events.php'页面并将数据附加到div。
$('.next-week').click(function(e) {
e.preventDefault();
$.ajax({
url: "http://gwsb-website/wp-content/themes/gwsb/get-events.php",
cache: false
}).done(function( html ) {
$('.events-listing').html( html );
});
});
这是'get-events.php'中的php
<!-- language: lang-php -->
<?php
define('WP_USE_THEMES', false);
require_once('../../../wp-load.php');
// Variables
$day = date('w');
$weekStart = date('Ymd', strtotime('monday this week'));
$weekEnd = date('Ymd', strtotime('sunday this week'));
$currentHeader = '';
query_posts(array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => array( $weekStart, $weekEnd ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
));
?>
<?php while( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<?php
$tempDate = get_post_meta( get_the_ID(), 'event_date', true );
if ( $tempDate != $currentHeader ) : $currentHeader = $tempDate; ?>
<h3 class="event-date-header"><?php $date = DateTime::createFromFormat('Ymd', $currentHeader); echo $date->format('l, F d, Y'); ?></h3>
<?php endif; ?>
<div class="event<?php echo the_subcategory_class(); ?>">
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<p><?php $content = get_the_content(); echo wp_trim_words( $content , '20' ); ?></p>
<div class="event-meta">
<?php if (get_field('start_time')) : ?>
<span><?php the_field('start_time'); ?></span>
<?php endif; ?>
<?php if (get_field('end_time')) : ?>
<span> - </span><span><?php the_field('end_time'); ?></span>
<?php endif; ?>
<?php if (get_field('location')) : ?>
<span class="dot">•</span><span><?php the_field('location'); ?></span>
<?php endif; ?>
</div><!-- .event-meta -->
</div><!-- .event -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>