使用Get_the_Terms时的属性排序顺序

时间:2014-03-14 作者:user48945

此代码用于在页面上显示产品属性。

如何按slug对其进行排序/排序?它们当前按名称排序。

 global $post;
 $terms = get_the_terms( $post->ID, \'pa_size\');
 foreach ( $terms as $term ) {
 echo "<li>" .$term->name. "</li>";
 }

1 个回复
SO网友:s_ha_dum

您需要自己对其进行排序:

 $terms = get_the_terms( $post->ID, \'category\');
 foreach ( $terms as $term ) {
  $newterms[$term->slug] = $term;
 }
 ksort($newterms);
 foreach ( $newterms as $term ) {
  echo "<li>" .$term->name. "</li>";
 }
或者,如果你喜欢冒险,也可以使用过滤器:

function alpha_sort_terms($terms) {
  remove_filter(\'get_the_terms\',\'alpha_sort_terms\');
  foreach ( $terms as $term ) {
    $newterms[$term->slug] = $term;
  }
  ksort($newterms);
  return $newterms;
}
add_filter(\'get_the_terms\',\'alpha_sort_terms\');

$terms = get_the_terms( $post->ID, \'category\');
foreach ( $terms as $term ) {
  echo "<li>" .$term->name. "</li>";
}

结束