尝试全球化$post
的内部event-item.php
.
另外:一定要打电话wp_reset_postdata()
在您关闭$loop
while循环。
e、 g.:
<!-- event-item.php -->
<?php
// globalize $post
global $post;
?>
<div id="event-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="event-date"><?php echo $event_date; ?></div>
<div class="event_time"><?php echo $event_time; ?></div>
<div class="event-speaker"><?php echo $event_speaker; ?></div>
<div class="event-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
<div class="event-description-excerpt"><?php the_excerpt(); ?></div>
</div>
<!-- event-item.php -->
然后:
<?php
endwhile;
// Restore $post global to the primary query
wp_reset_postdata();
?>
编辑假设问题只是post元数据,我建议将post元数据变量移动到内部
loop-item.php
. 只是猜测,但可能您的局部变量没有通过
include()
属于的函数
get_template_part()
.
所以,像这样:
<!-- event-item.php -->
<?php
// Define these here, inside loop-item.php
$wr_event_fields = get_post_custom();
$event_date_timestamp = $wr_event_fields[\'_wr_event_date\'][0];
$event_date = strftime(\'%d.%m.%Y\', $event_date_timestamp);
$event_time = $wr_event_fields[\'_wr_event_time\'][0];
$event_speaker = $wr_event_fields[\'_wr_event_speaker\'][0];
?>
<div id="event-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="event-date"><?php echo $event_date; ?></div>
<div class="event_time"><?php echo $event_time; ?></div>
<div class="event-speaker"><?php echo $event_speaker; ?></div>
<div class="event-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
<div class="event-description-excerpt"><?php the_excerpt(); ?></div>
</div>
<!-- event-item.php -->
Also, 避免
undefined variable
注意,您应该使用
isset()
条件句;e、 g.更改此项:
$event_date_timestamp = $wr_event_fields[\'_wr_event_date\'][0];
。。。为此:
$event_date_timestamp = ( isset( $wr_event_fields[\'_wr_event_date\'][0] ) ? $wr_event_fields[\'_wr_event_date\'][0] : false );