【问题标题】:Wordpress Events List Date ProblemWordpress 事件列表日期问题
【发布时间】:2011-01-17 21:51:37
【问题描述】:

我在 wordpress 中以正确的顺序显示事件时遇到问题。我认为问题在于 wordpress 将日期视为字符串并按天排序,因为它是英国日期格式。

目标是显示未来事件列表,最新事件位于列表顶部。但我必须使用 dd/mm/yyyy 的英国日期格式。

我需要回到绘图板上还是有办法转换日期以达到我需要的结果?

提前致谢:)

<ul>
<?php // Get today's date in the right format
$todaysDate = date('d/m/Y');?>

<?php  query_posts('showposts=50&category_name=Training&meta_key=date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC'); ?>

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <li>
      <h3><a href="<?php the_permalink(); ?>">
        <?php the_title(); ?>
        </a></h3>
        <?php $getDate = get_post_meta($post->ID, 'date', TRUE);
        $dateArray = explode('/', $getDate); ?>
        <?php if($getDate != '') { ?>
            <div class="coursedate rounded"><?php echo date('d F Y', mktime(0, 0, 0, $dateArray[1], $dateArray[0], $dateArray[2])); ?></div>
        <?php } ?>
        <p><?php get_clean_excerpt(140, get_the_content()); ?>...</p>
        <p><strong><a class="link" href="<?php the_permalink(); ?>">For further details and booking click here</a></strong></p>
    </li>
    <?php endwhile; ?>
    <?php else : ?>
        <li>Sorry, no upcoming events!</li>
<?php endif; ?>

【问题讨论】:

    标签: php string events wordpress date


    【解决方案1】:

    我建议您在 save_post 挂钩函数中将日期转换为 UNIX 格式 (mktime()),并在显示时使用 date( 'd/m/Y', $timestamp )。 UNIX 时间戳可以简单地按数字、升序或降序排序。

    如果做不到这一点,您将需要使用自定义 MySQL 查询,这并不理想(尽管完全可以接受)。

    参考:datemktime

    【讨论】:

    • 谢谢,我去看看。虽然作为临时修复,我安装了 post expirator,效果相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多