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


WordPress - Custom Query with List of Times, 1:00am is before 2:00pm - How to Order Correctly?

我在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:00等

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

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

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

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

<?=twentyfourToTwelve($postTime)?>

我最终使用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" );