Wordpress:按月分组的事件

时间:2010-08-27 16:46:19

标签: php mysql wordpress datetime date

在我的WP网站中,我有一个名为'events'的类别,我使用两个自定义字段发布事件信息:

  1. eventdate =人类可读事件日期
  2. eventsortdate = YYYY / MM / DD以正确的顺序列出事件。
  3. 我从这里有用的帖子中获得了这段代码: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; ?>
    

    这将确保事件以正确的顺序列出。但是,我还希望按月对事件进行分组 - 所以有一个“月”标题,并将所有属于该月标题的事件分组。

    任何非常感激的想法,或者如何实现这一点的替代建议也非常感激。感谢。

    编辑:

    修改后的代码考虑了建议的代码:

    <?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; ?>
    

    编辑:进一步修正,其功能正确:

    <?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; ?>
    

1 个答案:

答案 0 :(得分:1)

你可以使用WP_Query()函数做出惊人的事情,但我认为gruoping并不属于它。你可以做的是构建一个数组并输出该数组的结果。或者您可以保存当前月份并在下个月更改后立即输出:

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

这将打印第一个事件的月份编号(尚未设置$ surrentMonth),然后每当新月出现时再次。

但是你肯定必须改变你想要的输出(LI)。

相关问题