排序数组按字母顺序php/wordpress
问题描述:
如何使用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()) : ?>
答
事实上你可以使用PHP函数来对数组进行排序,这里是文档(http://php.net/manual/en/array.sorting.php)。但是,可以按标题对wp文章进行排序。这里是链接https://codex.wordpress.org/Alphabetizing_Posts
问候。 Ed。
+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()) :
?>
http://php.net/manual/en/function.sort.php – Calimero
如果要按字母顺序对结果进行排序,为什么要通过“menu_order”命令检索它们?为什么不在WP_Query的相关字段中指定字母顺序? – FluffyKitten