0

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

上午 1:00、上午 2:00、下午 1:00、下午 2:00、下午 3:00 等

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

上午 1:00、下午 1:00、上午 2:00、下午 2:00、上午 3:00、下午 3:00 等。

我需要它以正确的顺序显示它们,例如: 1: 00am、2:00am、3:00am....11:00am、12:00pm、1:00pm、2:00pm、3:00pm 等

我想过简单地切换到 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>
4

2 回答 2

0

正如 jprofit 在评论部分所说,为您的元值使用 24 小时制。然后,要格式化为 12 小时字符串,您需要执行以下操作:

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

以及你需要显示时间的地方,

<?=twentyfourToTwelve($postTime)?>
于 2012-05-22T05:52:41.957 回答
-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" );
于 2012-05-22T15:23:54.420 回答