I'm currently trying to get my events listing code to print out headers by Month. So basically this:


  • event
  • event


  • event
  • event


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.


    $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()) :

    $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>';


        <strong><a href="<?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 />

    <?php endwhile; else: ?>
    <ul id="events">
    <li><?php _e('No Events Yet .'); ?></li>
    <?php endif; ?>

1 Answer 1


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.