WordPress - 自定义查询与时间列表,凌晨1:00是下午2:00之前 - 如何正确订购?

时间:2012-05-22 01:27:48

标签: php wordpress

我在WordPress中有一个自定义查询,它将时间分配给自定义元字段中的帖子项。时间按以下格式输入:

凌晨1:00,凌晨2:00,下午1:00,下午2:00,下午3:00等

查询按时间顺序显示帖子,但是,它按顺序显示它们,如下所示:

凌晨1:00,下午1:00,凌晨2:00,下午2:00,凌晨3:00,下午3:00等

我需要它以正确的顺序显示它们,如下所示:凌晨1:00,凌晨2:00,凌晨3:00 ....上午11:00,中午12:00,下午1:00,下午2:00 ,下午3点等,

我想过简单地切换到24小时制,但有些用户并不熟悉它。

这是我的查询,有人可以帮我解决问题:

<ul>
<?php
$args=array(
  'taxonomy' => 'day',
  'term' => 'monday',
  'post_type' => 'schedule',
  'meta_key' => 'tr_show_time', 
  'orderby' => 'tr_show_time', 
  'order' => 'asc',  
  'posts_per_page' => 24,
  'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
      <?php $show_name = get_post_meta( get_the_ID(), 'tr_show_name', true ); ?>
      <?php $show_time = get_post_meta( get_the_ID(), 'tr_show_time', true ); ?>
      <li>
        <?php echo $show_time;?> - <?php echo $show_name;?>
      </li>
      <?php endwhile; } wp_reset_query(); ?>
</ul>

2 个答案:

答案 0 :(得分:0)

正如jprofit在评论部分所说,使用24小时制作你的元数值。然后,要格式化为12小时字符串,这是您需要做的:

<?php
function twentyfourToTwelve($time)
{
    return date('g:i:sa', strtotime($time));
}
?>

并且您需要显示时间,

<?=twentyfourToTwelve($postTime)?>

答案 1 :(得分:-2)

我最终使用jQuery来解决这个问题,就个人而言,我发现它比在PHP中更容易。

我使用了这个脚本和以下代码:

http://benalman.com/projects/jquery-replacetext-plugin/

$("#whats-on-container *").replaceText( /00:00/gi, "12:00am" );
$("#whats-on-container *").replaceText( /01:00/gi, "1:00am" );
$("#whats-on-container *").replaceText( /02:00/gi, "2:00am" );
$("#whats-on-container *").replaceText( /03:00/gi, "3:00am" );
$("#whats-on-container *").replaceText( /04:00/gi, "4:00am" );
$("#whats-on-container *").replaceText( /05:00/gi, "5:00am" );
$("#whats-on-container *").replaceText( /06:00/gi, "6:00am" );
$("#whats-on-container *").replaceText( /07:00/gi, "7:00am" );
$("#whats-on-container *").replaceText( /08:00/gi, "8:00am" );
$("#whats-on-container *").replaceText( /09:00/gi, "9:00am" );
$("#whats-on-container *").replaceText( /10:00/gi, "10:00am" );
$("#whats-on-container *").replaceText( /11:00/gi, "11:00am" );
$("#whats-on-container *").replaceText( /12:00/gi, "12:00pm" );
$("#whats-on-container *").replaceText( /13:00/gi, "1:00pm" );
$("#whats-on-container *").replaceText( /14:00/gi, "2:00pm" );
$("#whats-on-container *").replaceText( /15:00/gi, "3:00pm" );
$("#whats-on-container *").replaceText( /16:00/gi, "4:00pm" );
$("#whats-on-container *").replaceText( /17:00/gi, "5:00pm" );
$("#whats-on-container *").replaceText( /18:00/gi, "6:00pm" );
$("#whats-on-container *").replaceText( /19:00/gi, "7:00pm" );
$("#whats-on-container *").replaceText( /20:00/gi, "8:00pm" );
$("#whats-on-container *").replaceText( /21:00/gi, "9:00pm" );
$("#whats-on-container *").replaceText( /22:00/gi, "10:00pm" );
$("#whats-on-container *").replaceText( /23:00/gi, "11:00pm" );
$("#whats-on-container *").replaceText( /24:00/gi, "12:00am" );