首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试按日期对末尾为空值的一系列WordPress帖子进行排序

尝试按日期对末尾为空值的一系列WordPress帖子进行排序
EN

Stack Overflow用户
提问于 2019-01-03 20:36:01
回答 1查看 56关注 0票数 1

我正在尝试按开始日期对WordPress中的一系列帖子进行排序,这使用了一个自定义的帖子类型。输入日期是可选的,这意味着如果没有输入日期,则将其视为null。

但是,当未设置日期格式时,日期格式会自动默认为1970年1月1日,这意味着无论如何,没有开始日期的帖子都会在顶部而不是底部排序。颠倒顺序将不起作用,因为具有正确日期的事件将以错误的方式排序。

代码语言:javascript
复制
// 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事件在结尾而不是开头,我在网上找不到任何关于这方面的东西。

关于代码的其他一切都是有效的,它只是顺序。

耽误您时间,实在对不起

EN

回答 1

Stack Overflow用户

发布于 2019-01-03 21:00:11

无论如何,两个循环都不是更快的解决方案

代码语言:javascript
复制
//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;
                        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54022424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档