首页 > 解决方案 > 如何按字母顺序对转发器字段进行排序?ACF

问题描述

我想显示用户列表的名称并按用户名称的字母顺序排列它们。我怎样才能做到这一点?我正在寻求帮助来解决这个问题:

<?php if(get_field('sign_up',  'options')): ?>

<ul>

<?php while(has_sub_field('sign_up',  'options')): ?>

    <li><?php the_sub_field('your_name', 'options'); ?></li>

<?php endwhile; ?>

</ul>

<?php endif; ?>

标签: phpwordpressadvanced-custom-fields

解决方案


尝试以下操作,使我们使用 array_multisort() 函数:

<?php // Get repeater value
$repeater = get_field('sign_up',  'options');

// Obtain list of columns
foreach ($repeater as $key => $row) {
    $the_name[$key] = $row['your_name'];
}

// Sort the data by name column, ascending
array_multisort($the_name, SORT_ASC, $repeater);?>

<?php if($repeater): ?>
    <ul>
        <?php while(has_sub_field('sign_up',  'options')): ?>
            <?php foreach( $repeater as $row ) { // Display newly orded columns ?>
                <li><?php echo $row['your_name'];?></li>
            <?php } ?>
        <?php endwhile; ?>
    </ul>
<?php endif;?>

推荐阅读