我在Wordpress中创建了一个带自定义字段的自定义帖子类型。我创建了一个新的页面模板,它通过循环运行,效果很好。我正在尝试向主页的侧边栏添加类似的循环,但它无法正常工作。
首先,我自己的页面模板上的工作代码。
<h2>Upcoming Events</h2>
<?php $todaysDate = date('Y/m/d');
$event_query = new WP_query('post_type=events&posts_per_page=-1&post_status=publish&meta_key=event_date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC');
?>
<ul id="featured_event_list">
<?php if ($event_query->have_posts()): ?>
<?php while ($event_query->have_posts()) : $event_query->the_post(); ?>
<li>
<h3><?php the_title(); ?></h3>
<p>
<?php if ( get_post_meta($post->ID, 'event_featuring', true) ) : ?>
<strong>Featuring:</strong> <?php echo get_post_meta($post->ID, 'event_featuring', true) ?><br />
<?php endif; ?>
<?php if ( get_post_meta($post->ID, 'event_date', true) ) : ?>
<?php $event_date = get_post_meta($post->ID, 'event_date', true);
$new_event_date = date("F j, Y",strtotime($event_date)); ?>
<strong>Date:</strong> <?php echo $new_event_date ?>
<?php endif; ?>
<?php if ( get_post_meta($post->ID, 'event_end_date', true) ) : ?>
<?php $event_end_date = get_post_meta($post->ID, 'event_end_date', true);
$new_event_end_date = date("F j, Y",strtotime($event_end_date)); ?>
- <?php echo $new_event_end_date ?>
<?php endif; ?>
<br />
<?php if ( get_post_meta($post->ID, 'event_time', true) ) : ?>
<strong>Time:</strong> <?php echo get_post_meta($post->ID, 'event_time', true) ?><br />
<?php endif; ?>
<?php if ( get_post_meta($post->ID, 'event_location', true) ) : ?>
<strong>Location:</strong> <?php echo get_post_meta($post->ID, 'event_location', true) ?>
<?php endif; ?>
</p>
<p><?php the_excerpt(); ?></p>
</li>
<?php endwhile; ?>
<?php else: ?>
<li>Sorry, no upcoming events.</li>
<?php endif; ?>
</ul>
工作正常,但是当我将该代码的修改版本作为widget.php中的小部件放置时,它不起作用:
<?php
function eventsWidget() { ?>
<div class="uwf_widget">
<h3 class="events"><span>Featured Events</span></h3>
<ul>
<?php
$todaysDate = date('Y/m/d');
$event_widget_query = new WP_query('post_type=events&posts_per_page=2&post_status=publish&meta_key=event_date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC');
?>
<?php if ($event_widget_query->have_posts()) :?>
<?php while ($event_widget_query->have_posts()) : $event_widget_query->the_post(); ?>
<?php $event_date = get_post_meta($post->ID, 'event_date', true);
$new_event_date = date("F j",strtotime($event_date)); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?> - <?php echo $new_event_date; ?> »</a></li>
<?php endwhile; else: ?>
<li>Sorry, no upcoming events.</li>
<?php endif; ?>
</ul>
<p class="more"><a href="/about-us/events/">more events »</a></p>
</div>
<?php } ?>
现在,如果我将该代码放在正常的侧边栏中(而不是在主页上) - 它可以正常工作。所以,显然主页和侧边栏上的多个循环有问题。如果我简化小部件以便它只抓取2个“事件”,它将抓住两个帖子(无论post_type如何)。我试过rewind_posts和wp_reset_query,所以我不知道还有什么可以尝试?
答案 0 :(得分:1)
我遇到同样的问题,这是由wp_hook'pre_get_posts'引起的
<?php function custom_get_posts( $query ) {
if ( !is_admin() && false == $query->query_vars['suppress_filters'] )
$query->set( 'post_type', array('post', 'events', 'pages') );
return $query;
}
add_filter( 'pre_get_posts', 'custom_get_posts' );
?>
要解决此问题,您可以在WP_Query之前删除此过滤器,并在此查询后再次添加。
<?php
remove_filter( 'pre_get_posts', 'custom_get_posts' );
/*
* Your new WP_Query
*/
add_filter( 'pre_get_posts', 'custom_get_posts' );
?>
答案 1 :(得分:0)
尝试在功能开头添加global post;
声明。
<?php
function eventsWidget() {
global $post; ?>
<div class="uwf_widget">
<h3 class="events"><span>Featured Events</span></h3>
<ul>
<?php
$todaysDate = date('Y/m/d');
$event_widget_query = new WP_query('post_type=events&posts_per_page=2&post_status=publish&meta_key=event_date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC');
?>
<?php if ($event_widget_query->have_posts()) :?>
<?php while ($event_widget_query->have_posts()) : $event_widget_query->the_post(); ?>
<?php $event_date = get_post_meta($post->ID, 'event_date', true);
$new_event_date = date("F j",strtotime($event_date)); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?> - <?php echo $new_event_date; ?> »</a></li>
<?php endwhile; else: ?>
<li>Sorry, no upcoming events.</li>
<?php endif; ?>
</ul>
<p class="more"><a href="/about-us/events/">more events »</a></p>
</div>
<?php } ?>
答案 2 :(得分:0)
首先使用rewind_posts()
后的endwhile
,如果您的侧边栏调用位于模板底部,请在页面模板中执行,或者如果侧边栏是在顶部调用。)
另一种方法:
<?php
function eventsWidget() { ?>
<div class="uwf_widget">
<h3 class="events"><span>Featured Events</span></h3>
<ul>
<?php $todaysDate = date('Y/m/d');
query_posts('$post_type=event&posts_per_page=2');
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php while ($have_posts()) : the_post(); ?>
<?php $event_date = get_post_meta($post->ID, 'event_date', true);
$new_event_date = date("F j",strtotime($event_date)); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?> - <?php echo $new_event_date; ?> »</a></li>
<?php endwhile; else: ?>
<li>Sorry, no upcoming events.</li>
<?php endif; ?>
</ul>
<p class="more"><a href="/about-us/events/">more events »</a></p>
</div>
<?php } ?>
答案 3 :(得分:0)
我终于解决了这个问题。我使用了get_posts并构建了一个数组,由于某种原因它现在可以在主页上运行。
<?php
function eventsWidget() {?>
<div class="uwf_widget">
<h3 class="events"><span>Featured Events</span></h3>
<ul>
<?php
global $post;
$todaysDate = date('Y/m/d');
$event_args = array('post_type' => 'events',
'numberposts' => 2,
'meta_key' => 'event_date',
'post_status' => 'publish',
'meta_compare' => '>=',
'meta_value' => $todaysDate,
'orderby' => 'meta_value',
'order'=> 'ASC');
$event_widget_query = get_posts( $event_args );
foreach ($event_widget_query as $post) : setup_postdata($event_widget_query); ?>
<?php $event_date = get_post_meta($post->ID, 'event_date', true);
$new_event_date = date("F j",strtotime($event_date)); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?> - <?php echo $new_event_date; ?> »</a></li>
<?php endforeach; ?>
</ul>
<p class="more"><a href="/about-us/events/">more events »</a></p>
</div>
<?php wp_reset_query(); ?>
<?php } ?>