为什么不使用一个带参数的简单函数来实现这一点,代码如下所示:
function wpse63585_event_list( $fresh = true )
{
echo \'<ul class="event-items">\';
$yesterday = time() - 24*60*60;
$compare = $fresh ? \'>\' : \'<\';
$args = array(
\'post_type\' => \'wr_event\',
\'posts_per_page\' => -1, // show all posts
\'meta_key\' => \'event_date\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\',
\'meta_value\' => $yesterday,
\'meta_compare\' => $compare,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( \'inc/event\', \'item\' );
endwhile;
wp_reset_postdata();
echo \'</ul>\';
}
然后在你的
index.php
, 只需致电:
<?php wpse63585_event_list(); ?>
并且在
archives.php
:
<?php wpse63585_event_list( false ); ?>
Another solution 我想你可以注册一个全局变量,比如
$wpse63585_fresh = true
, 并在模板部分使用该变量
event-list.php
控制条件,如下所示:
在里面index.php
:
global $wpse63585_fresh;
$wpse63585_fresh = true;
get_template_part( \'inc/event\', \'list\' );
在中
archive.php
:
global $wpse63585_fresh;
$wpse63585_fresh = false;
get_template_part( \'inc/event\', \'list\' );
在中
event-list.php
:
<ul class="event-items">
<?php
global $wpse63585_fresh;
$compare = $wpse63585_fresh ? \'>\' : \'<\';
$yesterday = time() - 24*60*60;
$args = array(
\'post_type\' => \'wr_event\',
\'posts_per_page\' => -1, // show all posts
\'meta_key\' => \'event_date\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\',
\'meta_value\' => $yesterday,
\'meta_compare\' => $compare
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( \'inc/event\', \'item\' );
endwhile;
wp_reset_postdata();
?>
</ul>