我正在尝试按开始日期对WordPress中的一系列帖子进行排序,这使用了一个自定义的帖子类型。输入日期是可选的,这意味着如果没有输入日期,则将其视为null。
但是,当未设置日期格式时,日期格式会自动默认为1970年1月1日,这意味着无论如何,没有开始日期的帖子都会在顶部而不是底部排序。颠倒顺序将不起作用,因为具有正确日期的事件将以错误的方式排序。
// This gets all the posts and sorts them by date
$events = get_posts([
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'eventStarttime',
'orderby' => 'meta_value',
'order' => 'ASC'
]);
// The events are looped through in this foreach
foreach ($events as $event) {
echo "<button aria-controls='event-{$event->ID}' class='location-navigation-item js-location-button non-mobile'>";
$timestamp = get_field('eventStartTime', $event->ID);
$eventUnlocked = (get_field('eventActive') === "yes") ? true : false;
$date = strtr($timestamp, '/', '-');
echo "<h3>".strtoupper(get_the_title($event->ID))."</h3>";
if ($timestamp):
echo "<h4 class='minorheading'>". date('d', strtotime($date)). '<sup>' . date('S', strtotime($date)) . '</sup>/' . strtoupper(date('M', strtotime($date)))."</h4>";
else:
echo "<h4 class='minorheading'>TBC</h4>";
endif;
echo "</button>";
echo "<a href='".get_permalink($event->ID). "'>";
echo " <div class='location-navigation-item mobile'>";
echo " <div class='mobile-data'>";
echo " <h3>".strtoupper(get_the_title($event->ID))."</h3>";
echo " <h4 class='minorheading'>". date('d', strtotime($date)). '<sup>' . date('S', strtotime($date)) . '</sup>/' . strtoupper(date('M', strtotime($date)))."</h4>";
echo " </div>";
echo " <button aria-controls='event-{$event->ID}' class='long-button event-location-button js-event-button'></button>";
echo " </div>";
echo "</a>";
}很抱歉缩进不好,
正如您在时间戳if语句中看到的那样,当没有设置日期时,"TBC“将作为值列出,如果不添加,将显示1970年1月1日的日期。
我只是想让null事件在结尾而不是开头,我在网上找不到任何关于这方面的东西。
关于代码的其他一切都是有效的,它只是顺序。
耽误您时间,实在对不起
发布于 2019-01-03 21:00:11
无论如何,两个循环都不是更快的解决方案
//The events are looped through in this foreach
foreach($events as $event)
{
echo "<button aria-controls='event-{$event->ID}' class='location-navigation-item js-location-button non-mobile'>";
$timestamp = get_field('eventStartTime', $event->ID);
$eventUnlocked = (get_field('eventActive') === "yes") ? true : false;
$date = strtr($timestamp, '/', '-');
if ($timestamp):
echo "<h3>".strtoupper(get_the_title($event->ID))."</h3>";
echo "<h4 class='minorheading'>". date('d', strtotime($date)). '<sup>' . date('S', strtotime($date)) . '</sup>/' . strtoupper(date('M', strtotime($date)))."</h4>";
echo "</button>";
echo "<a href='".get_permalink($event->ID). "'>";
echo "<div class='location-navigation-item mobile'>";
echo "<div class='mobile-data'>";
echo "<h3>".strtoupper(get_the_title($event->ID))."</h3>";
echo "<h4 class='minorheading'>". date('d', strtotime($date)). '<sup>' . date('S', strtotime($date)) . '</sup>/' . strtoupper(date('M', strtotime($date)))."</h4>";
echo "</div>";
echo "<button aria-controls='event-{$event->ID}' class='long-button event-location-button js-event-button'></button>";
echo "</div>";
echo "</a>";
endif;
}
reset ($events);
foreach($events as $event1)
{
echo "<button aria-controls='event1-{$event1->ID}' class='location-navigation-item js-location-button non-mobile'>";
$timestamp = get_field('eventStartTime', $event1->ID);
$eventUnlocked = (get_field('eventActive') === "yes") ? true : false;
$date = strtr($timestamp, '/', '-');
if (!$timestamp):
echo "<h3>".strtoupper(get_the_title($event->ID))."</h3>";
echo "<h4 class='minorheading'>TBC</h4>";
echo "</button>";
echo "<a href='".get_permalink($event1->ID). "'>";
echo "<div class='location-navigation-item mobile'>";
echo "<div class='mobile-data'>";
echo "<h3>".strtoupper(get_the_title($event1->ID))."</h3>";
echo "<h4 class='minorheading'>". date('d', strtotime($date)). '<sup>' . date('S', strtotime($date)) . '</sup>/' . strtoupper(date('M', strtotime($date)))."</h4>";
echo "</div>";
echo "<button aria-controls='event-{$event1->ID}' class='long-button event-location-button js-event-button'></button>";
echo "</div>";
echo "</a>";
endif;
}https://stackoverflow.com/questions/54022424
复制相似问题