download2565 2016-02-27 19:09
浏览 148
已采纳

高级自定义字段Wordpress Repeater字段按日期排序

I am using advanced custom fields pro to store and display information on estate sale dates. These are held in a repeater field, each row in the field has a start time, end time and a date. I need to display the information ordered by the date in the repeater field row. Currently it is ordered by date modified. I have tried several solutions but none seem to work for what I need to do.

This is what I have currently:

<?php
$latestes = new WP_Query(
    array(
        'post_type' => 'estate-sales',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'orderby' => 'modified',
        'order' => 'DESC'
        )
    );

while ( $latestes->have_posts() ) : $latestes->the_post();
    echo '<li class="frontpage-esale"><a href="';
    the_permalink();
    echo '">';
    the_title();
    echo '&nbsp;';
    $rowses = get_field('estate_sale_dates');
    $first_row = $rowses[0];
    $first_row_date = $first_row['es-date'];
    $first_row_start = $first_row['es-start-time'];
    $first_row_end = $first_row['es-end-time'];
    echo '&nbsp;-&nbsp;';
    if ($first_row_date) {
        echo date('F j, Y',strtotime($first_row_date));
    }
    else {
    echo 'Sale Dates TBA';
    }
endwhile;
wp_reset_query();
?>

The most common answer when searching for a solution is to do something like this:

$latestes = new WP_Query(
    array(
        'post_type' => 'estate-sales',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'orderby' => 'modified',
        'order' => 'DESC'
        )
    );

while ( $latestes->have_posts() ) : $latestes->the_post();
$repeater = get_field('estate_sale_dates');
    foreach( $repeater as $key => $row )
    {
        $column_id[ $key ] = $row['es-date'];
    }
    array_multisort( $column_id, SORT_ASC, $repeater );
    foreach( $repeater as $row ) {
    echo '<li class="frontpage-esale"><a href="';
    the_permalink();
    echo '">';
    the_title();
    echo '&nbsp;';
    $rowses = get_field('estate_sale_dates');
    $first_row = $rowses[0];
    $first_row_date = $first_row['es-date'];
    $first_row_start = $first_row['es-start-time'];
    $first_row_end = $first_row['es-end-time'];
    echo '&nbsp;-&nbsp;';
    if ($first_row_date) {
        echo date('F j, Y',strtotime($first_row_date));
    }
    else {
    echo 'Sale Dates TBA';
    }
    }
endwhile;
wp_reset_query();
?>

However, when I try that, it lists each sale 3 times, does not order by the date at all, and if no date is entered (which needs to just default to Sale Dates TBA) it throws an error.

Any help greatly appreciated.

  • 写回答

2条回答 默认 最新

  • duanjiati1755 2016-02-27 20:46
    关注

    Note, that your integration isn't complete, signaled by having two get_field('estate_sale_dates');, and the name $first_row being out of place when iterating all of them. Without the sort, you're basically doing

      $repeater = get_field( 'estate_sale_dates' );
      ...
      foreach ( $repeater => $row_that_isnt_used )
      {
         $rowses = get_field( 'estate_sale_dates' );
         $first_row = $rowses[0]; 
      }
    

    I think something like this is what you're after:

    <?php
    while ( $latestes->have_posts() ) {
                    $latestes->the_post();
        echo '<li class="frontpage-esale"><a href="';
        the_permalink();
        echo '">';
        the_title();
        echo '</a> ';
    
        if ( ! count( $sale_dates = get_field('estate_sale_dates') ) )
            echo '<span>Sale Dates TBA</span>';
        else
        {
            // $order = array_column( $sale_dates, 'es-date' ): (php 5.5+)
            foreach( $sale_dates as $ignore => $row )
                $order[] = $row['es-date'];
    
            array_multisort( $order, SORT_ASC, $sale_dates );
    
            echo "<ul>";
            foreach( $sale_dates as $row ) {
                $date  = $row['es-date'];
                $start = $row['es-start-time'];
                $end   = $row['es-end-time'];
                echo "<li>" . date('F j, Y',strtotime($row_date)) . " $start-$end</li>";
            }
            echo "</ul>";
        }
    
        echo "</li>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?