我目前正在为一个电台网站编写一个空中部分的代码,并试图显示当前空中节目的标题和 meta数据。
为此,我创建了自定义字段Start Time
和End Time
,并设置了一个query_posts命令,以便根据当前时间大于Start Time
且小于End Time
的值来检索show post。
date_default_timezone_set('Europe/London');
$time = date("G:i");
$showstart = get_post_meta($post->ID, 'Start Time', true);
$showend = get_post_meta($post->ID, 'End Time', true);
if($time<$showstart)
{$startnext = $showstart;}
if($showstart<=$time && $time<$showend)
{$starttime = $showstart;}
if( date( 'w' ) == 0 ) {
query_posts('cat=17&posts_per_page=1&offset=0&orderby=meta_value_num& meta_value_num=$starttime&meta_key=Start Time&order=ASC');
}
else if( date( 'w' ) == 1 ) {
query_posts('cat=18&posts_per_page=1&offset=0&orderby=meta_value_num&meta_value_num=$starttime&meta_key=Start Time&order=ASC');
}
else if( date( 'w' ) == 2 ) {
query_posts('cat=12&posts_per_page=1&offset=0&orderby=meta_value_num&meta_value_num=$starttime&meta_key=Start Time&order=ASC');
}
else if( date( 'w' ) == 3 ) {
query_posts('cat=13&posts_per_page=1&offset=0&orderby=meta_value_num&meta_value_num=$starttime&meta_key=Start Time&order=ASC');
}
else if( date( 'w' ) == 4 ) {
query_posts('cat=14&posts_per_page=1&offset=0&orderby=meta_value_num&meta_value_num=$starttime&meta_key=Start Time&order=ASC');
}
else if( date( 'w' ) == 5 ) {
query_posts('cat=15&posts_per_page=1&offset=0&orderby=meta_value_num&meta_value_num=$starttime&meta_key=Start Time&order=ASC');
}
else if( date( 'w' ) == 6 ) {
query_posts('cat=16&posts_per_page=1&offset=0&orderby=met a_value_num&meta_value_num=$starttime&meta_key=Start Time&order=ASC');
}
posts循环如下所示:
// The Loop
while ( have_posts() ) : the_post();?>
<div id="listenlivebanner">
<div id="showwrap">
<div id="showdetails">
<div id="nowplayingimg">
<div>
<div style="height: 150px; display:block; margin-left: auto; margin-right: auto;"><?php the_post_thumbnail('onair-thumb', array('title' => ''.get_the_title().'' )); ?></div>
</div>
</div>
<div class="listenlive">
<h3>LIVE:</h3><p class="showname"><?php $short_title = substr(the_title('','',FALSE),0,18);
echo $short_title;
if (strlen($short_title) > 17){
echo '...';
} ?></p>
但是,目前它只是按照我设定的顺序检索第一个帖子,而不是根据当前时间进行更改。如何使它工作?
编辑:
从query_posts更改为新的WP_Query,但仍然没有运气-现在它只显示页面的标题,而不是我要检索的帖子的标题...
下面是代码:
else if( date( 'w' ) == 3 ) {
new WP_Query( array( 'cat' => 13, 'posts_per_page' => 1, 'offset' => 0, 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_query' => array(
array('key' => 'Start_Time', 'value' => '$time', 'compare' => '<=', 'type' => 'TIME' )
)
)
);
}
else if( date( 'w' ) == 4 ) {
new WP_Query( array( 'cat' => 14, 'posts_per_page' => 1, 'offset' => 0, 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_query' => array(
array('key' => 'Start_Time', 'value' => '$time', 'compare' => '<=', 'type' => 'TIME' )
)
)
);
}
1条答案
按热度按时间a8jjtwal1#
我会对您的设置进行更改:
start_time
和end_time
get_posts
或WP_Query
,但决不能使用query_posts
。请参见When should you use WP_Query vs query_posts() vs get_posts()?一个月五个月一个月
$query = new WP_Query( 'meta_key=color&meta_value=blue' );