【问题标题】:Displaying posts from custom post type and sorting them by starting time custom field, grouping them by day显示自定义帖子类型的帖子并按开始时间自定义字段对其进行排序,按天分组
【发布时间】:2017-02-13 21:39:06
【问题描述】:

我有 html 模板,我必须从中创建自定义 Wordpress 主题 - 我主要使用高级自定义字段,我需要以我在标题中解释的方式显示帖子(显示来自自定义帖子类型的帖子并按开始时间自定义字段,按天分组)。这是帖子实际外观的图像。 here's the image link

这是我定义的字段。 defined custom fields

因此,如果我在星期一有 3 场研讨会,它们会在星期一下展示,并且会按照开始时间从前开始排序。对于研讨会演讲者,我正在使用关系字段,该字段允许我将一个或多个演讲者(也是自定义帖子类型)添加到研讨会自定义帖子类型(我还没有弄清楚,但我会)。

我对排序有概念 - 类似于$arrayname[date][startingtime] = array(all the data that needs to be displayed),但我对如何在拉动 Workshops 的实际查询中实现它的想法为 0(也许与 foreach 循环有关?)。

所以基本上这是我现在拥有的代码,以及在卡住之前我已经走了多远。

<?php 

$args = array('post_type' => 'workshop');

$workshoptime = the_field('time');
$yeardate = the_field('workshop_date');

$sorting = array(
  array('time' => '$workshoptime'), 
  array('yearmonthdate' => '$yeardate'),
);

$loop = new WP_Query( $args );

//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();

?>

            <!--Tabs Box-->
            <div class="tabs-box">

                <!--Tab / Current / Monday-->
                <div class="tab current" id="monday">

                    <div class="hour-box active-box">
                        <div class="hour">10:00 AM</div>
                        <div class="img-circle circle"><span></span></div>
                        <div class="toggle-btn active"><h3><?php the_title();?></h3></div>
                        <div class="content-box collapsed">
                            <div class="content"><p><?php the_content();?></p></div>
                            <br>
                            <div class="row professional clearfix">
                                <div class="col-md-6 col-sm-6 col-xs-12 info">
                                    <figure class="img-circle image"><img class="img-circle" src="images/resource/testi-thumb-1.jpg" alt=""></figure>
                                    <h5 class="prof-title"></h5>
                                    <h6 class="prof-occup">Speaker</h6>
                                </div>
                                <div class="col-md-6 col-sm-6 col-xs-12 text-right">

                                    <a href="#" class="theme-btn btn-style-one hvr-bounce-to-right dull">10:00 - 12:00</a>
                                    <a href="single-event.html" class="theme-btn btn-style-one hvr-bounce-to-right"><span class="fa fa-play"></span>DETAILS ABOUT THE EVENT</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

【问题讨论】:

    标签: php wordpress sorting advanced-custom-fields


    【解决方案1】:

    您可以使用WP_Query对项目进行排序:

    $args = array(
        'posts_per_page'   => -1,       
        'orderby'          => 'meta_value_datetime',
        'order'            => 'ASC',
        'meta_type'        => 'DATETIME',
        'meta_key'         => 'workshop_date',      
        'post_type'        => 'workshop',       
        'post_status'      => 'publish'     
    );
    

    现在你在循环中有正确的顺序。

    分组方式可能如下所示:

    // in loop
    $timestamp = strtotime( $workshop_date );
    $date = date( 'Y-m-d', $timestamp );
    
    if ( $date == $current_date ) {
        // it is the same day
    } else {
        // it is a new day
        $current_date = $date;
    }
    

    【讨论】:

    • 也许(但我不知道)最好使用 datetime。
    【解决方案2】:

    您必须在您的 PHP 脚本中进行分组(循环浏览帖子,您可以检测日期何时更改,并输出相应的部分页眉/页脚)。为此,您需要按日期和时间对帖子进行排序,这可以通过常规 WP_Query 来实现,但您需要使用文档不是很清楚的 meta_query 功能。

    这是一个简单的例子:

    <?php
    
    $args = [
        'post_type' => 'workshop',
        'meta_query' => [
            'workshop_date' => [
                'key' => 'workshop_date', 
                'compare' => 'EXISTS'
            ],
            'workshop_time' => [
                'key' => 'workshop_time', 
                'compare' => 'EXISTS'
            ]
        ], 
        'orderby' => [
            'workshop_date' => 'ASC',
            'workshop_time' => 'ASC'
        ]
    ];
    $loop = new WP_Query($args);
    
    
    function print_workshop() {
    ?>
        <section class="workshop">
            <?php the_title() ?> at <?php the_field('workshop_time') ?>
        </section>
    <?php
    }
    
    function print_day_header() {
    ?>
    <article>
        <header><?php the_field('workshop_date') ?></header>
    <?php
    }
    
    function print_day_footer() {
    ?>
    </article>
    <?php
    }
    
    $curday = false;
    if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();
    
    if($curday !== get_field('workshop_date')) {
        // start a new day
        if($curday) {
            print_day_footer();
        }
        print_day_header();
        $curday = get_field('workshop_date');
    }
    // content
    print_workshop();
    
    endwhile; 
    
    // close the last one
    print_day_footer();
    
    else : ?>
    
    <article id="post-not-found" class="hentry cf">
        Text for no workshop found
    </article>
    
    <?php endif; ?>
    

    这会产生以下 HTML:

    <article>
        <header>06/10/2016</header>
        <section class="workshop">
            Workshop on the 6th at 11:00    </section>
    </article>
    <article>
        <header>13/10/2016</header>
        <section class="workshop">
            Workshop before the others on the 13th at 08:00    </section>
        <section class="workshop">
            Workshop 1 at 10:00    </section>
        <section class="workshop">
            Workshop 2, same day at 12:00    </section>
    </article>
    <article>
        <header>14/10/2016</header>
        <section class="workshop">
            Workshop 3, next day at 10:00    </section>
    </article>
    <article>
        <header>01/11/2016</header>
        <section class="workshop">
            A workshop in November at 09:00    </section>
    </article>
    

    最后一件事 - 如果您将时间作为文本存储在数据库中,请以标准化方式存储(即“13:00”,而不是“1 pm”),否则将无法正确排序。你可以用 ACF 做到这一点。 ACF 已经为日期字段处理了这个问题,因此您不必担心那个,只需担心时间。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      相关资源
      最近更新 更多