根据当前时间获取帖子

时间:2013-09-01 21:14:08

标签: wordpress post time

我正在为无线电网站编写一个播出节目,并试图显示当前节目的标题和元数据。

为此,我创建了自定义字段Start TimeEnd Time,并设置了query_posts命令,以根据当前时间比Start Time更大的值检索展示帖子并且小于End Time

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');
    }

帖子循环如下所示:

// 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 个答案:

答案 0 :(得分:0)

我在你的设置中改变了什么: