Wordpress Events List Date Problem
I'm having a problem displaying events in the correct order in wordpress. I think the problem is because wordpress is treating the date as a string and ordering it by the day because it's in british date format.
The goal is to display a list of future events with the most current event at the top of the list. But I must use the british date format of dd/mm/yyyy.
Do I need to go back to the drawing board or is there a way of converting the date to achieve the result I need?
Thanks in advance :)
<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,开发者_如何学JAVA '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; ?>
I would recommend you convert the date to UNIX format (mktime()
) within your save_post hook function, and use date( 'd/m/Y', $timestamp )
when you're displaying it. UNIX timestamps can be sorted simply numerically, ascending or descending.
Failing this, you would need to use a custom MySQL query, and that's not ideal (although perfectly acceptable).
Referenced: date and mktime.
精彩评论