I’m currently trying to get my events listing code to print out headers by Month. So basically this:
October
- event
- event
September
- event
- event
etc…
Right now it just keeps echoing January. I think it might have something to do with the $currentMonth or the strtotime() but I’m not sure what. Any suggestions? I’ve been looking at this post to try and figure it out.
Thanks!
<?php
$args = array(
'post_type' => 'events',
'orderby' => 'meta_value',
'meta_key' => 'tf_events_startdate',
'order' => 'DESC'
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
$custom = get_post_custom(get_the_ID());
// dates
$sd = $custom["tf_events_startdate"][0];
$ed = $custom["tf_events_enddate"][0];
// - local time format -
$time_format = get_option('time_format');
$stime = date($time_format, $sd);
$etime = date($time_format, $ed);
// set headers
if(!isset($currentMonth) || $currentMonth != date("m", strtotime($sd))){
$currentMonth = date("m", strtotime($sd));
echo '<h2>'.date("F", strtotime($sd)).'</h2>';
}?>
<ul>
<li>
<strong><a href="https://wordpress.stackexchange.com/questions/65201/<?php the_permalink();?>"><?php the_title();?></a></strong>
<br />
<?php // - determine if it's a new day -
$longdate = date("l, M j, Y", $sd);
if ($daycheck == null) { echo $longdate; }
if ($daycheck != $longdate && $daycheck != null) { echo $longdate; } ?>
<br />
<?php echo $stime;?>
<hr />
</li>
</ul>
<?php endwhile; else: ?>
<ul id="events">
<li><?php _e('No Events Yet .'); ?></li>
</ul>
<?php endif; ?>
1 Answer
One thing I noticed: Are $daycheck
and $currentMonth
defined or initiated previously? Also when you created your custom post type did you make sure 'has_archive' => true,
was included? I would go through and echo out all of your variables and see why January keeps coming up. My guess is that it has something to do with $custom["tf_events_startdate"][0]
As [0] might be January, [1] February etc.