2017-09-14 31 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()) : 
?>