如何按字母顺序对GET_TERM_CHILD输出进行排序

时间:2013-07-02 作者:Chris

下面创建了自定义分类法子项的下拉列表,但输出顺序似乎是按创建日期,而不是按字母名称。我尝试将orderby添加到阵列中,但没有成功。

我读过关于usort的文章,但不知道如何实现它。

如果有任何帮助,我将不胜感激。

$term_id = 61;
$taxonomy_name = \'tribe_events_cat\';
$termchildren = get_term_children( $term_id, $taxonomy_name );

echo "<script type=\'text/javascript\'>function taclinkch(x){var y=document.getElementById(x).value;document.location.href=y}</script>";
echo "<form action=\'\'>  <select name=\'eventcats\' id=\'eventcats\'   onchange=\'taclinkch(this.id)\'>";

echo "<option value=\'\'>Select a Topic</option>";

foreach ( $termchildren as $child ) {
    $term = get_term_by( \'id\', $child, $taxonomy_name );

    echo "<option value=\'" .  get_site_url() . "/events/category/". $term->slug." \'>" . $term->name . "</option>";
}

echo "</select></form>";
我添加了这个数组,但它不起作用:

foreach ( $termchildren as $child ) { 
    $term = get_term_by( 
        \'id\', 
        $child, 
        $taxonomy_name, 
        array( 
            \'orderby\' => \'name\',
            \'hide_empty\' => 0
        ) 
    );
}

1 个回复
SO网友:s_ha_dum

根据您的评论(并希望对问题进行编辑):

我是这样做的:foreach ( $termchildren as $child ) { $term = get_term_by( \'id\', $child, $taxonomy_name, array( \'orderby\' => \'name\', \'hide_empty\' => 0) );

已格式化:

foreach ( $termchildren as $child ) { 
  $term = get_term_by( 
    \'id\', 
    $child, 
    $taxonomy_name, 
    array( 
      \'orderby\' => \'name\',
      \'hide_empty\' => 0
    ) 
  );
get_term_by 不接受数组作为第四个参数,或者实际上不接受任何参数。您不能只是构建参数并期望它们工作。你需要看看Codexthe source.

此外get_term_by 而不是在这里进行排序。运行时会进行排序get_term_children, 尽管如此does not take a parameter that will order the results.

您可以对结果集进行排序:

$term_id = 61;
$taxonomy_name = \'tribe_events_cat\';
$termchildren = get_term_children( $term_id, $taxonomy_name );
$children = array();
foreach ($termchildren as $child) {
  $term = get_term_by( \'id\', $child, $taxonomy_name );
  $children[$term->name] = $term;
}
ksort($children);
// now $children is alphabetized
echo "<script type=\'text/javascript\'>function taclinkch(x){var y=document.getElementById(x).value;document.location.href=y}</script>";
echo "<form action=\'\'>  <select name=\'eventcats\' id=\'eventcats\'   onchange=\'taclinkch(this.id)\'>";
  echo "<option value=\'\'>Select a Topic</option>";
  foreach ( $children as $child ) {
    $term = get_term_by( \'id\', $child->term_id, $taxonomy_name );
    echo "<option value=\'" .  get_site_url() . "/events/category/". $term->slug." \'>" . $term->name . "</option>";
  }
echo "</select></form>";

结束

相关推荐

WP_GET_POST_TERMS按不工作排序

wp\\U get\\U post\\U条款订单不工作我的代码在这里query_posts(array(\'post_type\' => \'product\',\'post__in\' => $product_ids,)); while (have_posts()) : the_post(); global $post; $args = array(\'orderby\' => \'name\', \'order\' => \'