[WordPress]按术语获取帖子并对其排序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[WordPress]按术语获取帖子并对其排序相关的知识,希望对你有一定的参考价值。
1. Get Posts however you see fit2. Loop through the posts getting the terms from taxonomies and add them to the Post object
3. Sort the Array of Post Objects by the property you want them sorted by using sort_on_field()
<?php 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC' ); foreach( get_posts($args) as $post ): //Works if there is only one term tagged in the post for this taxonomy. You need to figure out how best to handle multiple terms. You might try imploding() them in a comma seperated list: "Term 1, Term 2" $post[$term->taxonomy] = $term->name; } $posts[] = (object) $post; endforeach; /** * Sort array of objects by field. * * @param array $objects Array of objects to sort. * @param string $on Name of field. * @param string $order (ASC|DESC) **/ function sort_on_field(&$objects, $on, $order = 'ASC') { $comparer = ($order === 'DESC') ? "return -strcasecmp($a->{$on},$b->{$on});" : "return strcasecmp($a->{$on},$b->{$on});"; } /* Use `strcmp` for case-sensitive string comparing. Use `strcasecmp` for case-insensitive string comparing. Source: http://www.php.net/manual/de/function.usort.php#104873 */ sort_on_field($posts, 'taxonomy1', 'DESC'); foreach( $posts as $post ) { echo $post->taxonomy1; } ?>
以上是关于[WordPress]按术语获取帖子并对其排序的主要内容,如果未能解决你的问题,请参考以下文章
在 Wordpress 后端保存 jQuery UI 可排序顺序