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 linux下点对点协议连接2个USB串口的硬件流量控制问题
  • ¥15 SQL数据自动生成问题
  • ¥15 设密码后无法访问他人的共享文件
  • ¥15 浏览器打不开学校网页
  • ¥100 android开发打不开相册,不弹出授权信息,请问怎么解决?
  • ¥15 我想通过python处理sybase IQ库的一些数据,但是不知道sybase IQ库怎么连接,之前连接都是用linux上的 dbisql
  • ¥15 安装llama-cpp-python==0.1.83失败
  • ¥20 管道轴向耦合水击问题
  • ¥60 补全networkx TODO部分。
  • ¥15 有内推吗,云计算linux运维方向