【问题标题】:Wordpress: events grouped by monthWordpress:按月分组的事件
【发布时间】:2010-08-31 20:03:23
【问题描述】:

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

  1. eventdate = 人类可读的事件日期
  2. eventsortdate = YYYY/MM/DD 按正确顺序列出事件。

我从一个有用的帖子中获得了这段代码: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; ?>

【问题讨论】:

    标签: php mysql wordpress datetime date


    【解决方案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)更改为您想要的。

    【讨论】:

    • 谢谢你 - 我得到的只是页面顶部显示的“01”。我已经在上面发布了我修改后的代码,有什么想法为什么会产生这个结果?
    • 日期('m')只是一个例子。使用“m”将输出月份的编号,而“F”将输出月份的名称。在此处查看 php 文档:php.net/manual/de/function.date.php
    • 嘿,非常感谢。通过一些调整,我已经让它工作了。非常感谢!! (见上面的调整代码)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多