开发者

Wordpress: events grouped by month

Within my WP site I have a category called 'events' where I am publishing event information using two custom fields:

  1. eventdate = human readable event date
  2. eventsortdate = YYYY/MM/DD to list events in the correct order.

I have this bit of code from a helpful post here: http://www.davidrisley.com/events-list-with-wordpress/

<?php
$timecutoff = date("Y-m-d");
$args = array(
'category_name' => 'events',
'orderby' => 'meta_value',
'meta_key' => 'eventsortdate',
'meta_compare' => '>=',
'meta_value' => $timecutoff,
'order' => 'DESC'
);
$my_query = new WP_Query($args);
if ($my_query开发者_运维问答->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
$eventdate = get_post_meta($post->ID, "eventdate", true);
?>
<ul id="events">
<li>
<strong><?php echo $eventdate; ?></strong><br />
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
</li>
</ul>
<?php endwhile; else: ?>
<ul id="events">
<li><?php _e('No Events Scheduled! Stay Tuned.'); ?></li>
</ul>
<?php endif; ?>

This will ensure events are listed in the correct order. However, I would also like to group events by month - so have a header of "month" and group all events which fall into that month display under that title.

Any ideas much appreciated, or alternative suggestions for how to achieve this also much appreciated. Thanks.

EDIT:

Amended code taking into account suggested code:

<?php
$timecutoff = date("Y-m-d");
$args = array(
'category_name' => 'events-press',
'orderby' => 'meta_value',
'meta_key' => 'eventsortdate',
'meta_compare' => '>=',
'meta_value' => $timecutoff,
'order' => 'ASC'
);
$my_query = new WP_Query($args);

if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
$eventdate = get_post_meta($post->ID, "eventdate", true);
?>

<?php if(!isset($currentMonth) || $currentMonth != date("m", strtotime($eventdate))){
    $currentMonth = date("m", strtotime($eventdate));
?>
<li><?php echo date("m", strtotime($eventdate)); ?></li>
<?php
}
?>

<ul>
    <li>
        <h5><?php echo $eventdate; ?></h5>
        <h4><?php the_title(); ?></h4>
        <?php the_content(); ?>
    </li>
</ul>
<?php endwhile; else: ?>
<ul id="events">
<li><?php _e('No Events Scheduled! .'); ?></li>
</ul>
<?php endif; ?>

EDIT: further amendment which functions correctly:

<?php
$timecutoff = date("Y-m-d");
$args = array(
'category_name' => 'events-press',
'orderby' => 'meta_value',
'meta_key' => 'eventsortdate',
'meta_compare' => '>=',
'meta_value' => $timecutoff,
'order' => 'ASC'
);
$my_query = new WP_Query($args);

if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
$eventdate = get_post_meta($post->ID, "eventdate", true);
$eventsortdate = get_post_meta($post->ID, "eventsortdate", true);
?>

<?php if(!isset($currentMonth) || $currentMonth != date("m", strtotime($eventsortdate))){
    $currentMonth = date("m", strtotime($eventsortdate));
?>
<li><?php echo date("F", strtotime($eventsortdate)); ?></li>
<?php
}
?>

<ul>
    <li>
        <h5><?php echo $eventdate; ?></h5>
        <h4><?php the_title(); ?></h4>
        <?php the_content(); ?>
    </li>
</ul>
<?php endwhile; else: ?>
<ul id="events">
<li><?php _e('No Events Scheduled! .'); ?></li>
</ul>
<?php endif; ?>


You can do amazing things with the WP_Query() function, but I think gruoping doesn't belongs to it. What you could do is building yourself an array and outputting the results from that array. Or you can just save the current month and output the next month as soon as it changes:

if(!isset($currentMonth) || $currentMonth != date("m", strtotime($eventdate))){
    $currentMonth = date("m", strtotime($eventdate));
?>
<li><?php echo date("m", strtotime($eventdate)); ?></li>
<?php
}

This will print put the month number for the first event ($surrentMonth has not been set, yet) and then again every time a new month is present.

But for sure you have to change the output (the LI) for what you want it to be.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜