2017-09-14 99 views
0

如何使用PHP在Wordpress CMS上按字母顺序排列数组?排序数组按字母顺序php/wordpress

<?php if(count($terms_array) > 0) : foreach($terms_array as $term) : ?> 

<?php 
    $term = ''; 
    $args = array(
     'post_type' => 'book', 
     'posts_per_page' => -1, 
     'orderby' => 'menu_order', 
     'order' => 'ASC' 
    ); 

    $books_query = new WP_Query($args); 
?> 

<?php if ($books_query->have_posts()) : ?> 
+1

http://php.net/manual/en/function.sort.php – Calimero

+0

如果要按字母顺序对结果进行排序,为什么要通过“menu_order”命令检索它们?为什么不在WP_Query的相关字段中指定字母顺序? – FluffyKitten

回答

0

在php中使用默认排序功能。假设其要排序的$ terms_array,

<?php 
sort($terms_array); 

if(count($terms_array) > 0) : 

foreach($terms_array as $term) : 

     $term = ''; 
     $args = array(
      'post_type' => 'book', 
      'posts_per_page' => -1, 
      'orderby' => 'menu_order', 
      'order' => 'ASC' 
     ); 
     $books_query = new WP_Query($args) 

     if ($books_query->have_posts()) : 
?>