如何按字母顺序对分类术语列表进行排序?

时间:2014-09-18 作者:Troy Templeman

我有以下代码在下拉列表中列出分类术语。它工作得很好,只是下拉列表中的列表不是按字母顺序排列的。如何修改代码以按字母顺序列出它们?

<form action="<?php bloginfo(\'url\'); ?>/" method="get">
<?php
$term_id = 279;
$taxonomy_name = \'categories\';
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo \'<select name="\' . $taxonomy_name . \'" onchange="this.form.submit()">\';
echo \'<option selected>Branding...</option>\';
foreach ( $termchildren as $child ) {
    $term = get_term_by( \'id\', $child, $taxonomy_name );
    $link = get_term_link( $child, $taxonomy_name );
    echo \'<option value="\'.$term->slug.\'"><a href="\' .esc_url( $link ) . \'">\' . $term->name . \'</a></option>\';
}
echo \'</select>\';
?> 
<noscript><div><input type="submit" value="View" /></div></noscript>
</form>

1 个回复
SO网友:Troy Templeman

我在上找到了答案https://wordpress.stackexchange.com/a/105079/40536

我将代码修改为以下内容:

<form action="<?php bloginfo(\'url\'); ?>/" method="get">

<?php
$term_id = 279;
$taxonomy_name = \'categories\';
$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);
echo \'<select name="\' . $taxonomy_name . \'" onchange="this.form.submit()">\';
echo \'<option selected>Branding...</option>\';
foreach ( $children as $child ) {
  $term = get_term_by( \'id\', $child->term_id, $taxonomy_name );
  echo \'<option value="\'. $term->slug .\'">\' . $term->name . \'</a></option>\';
}
echo \'</select>\';
?> 
<noscript><div><input type="submit" value="View" /></div></noscript>

</form>

结束

相关推荐

如何执行一个SQL查询来排除表wp_Terms中除自定义分类术语之外的所有术语?

问题摘要:我正在尝试编写一个只返回自定义分类术语的SQL查询,但该表(wp\\u terms)包含所有术语,甚至包括我不需要的术语(例如类别术语和nav\\u菜单术语)。This is my present query:function myajax_inputtitleSubmit_func() { global $wpdb; global $customTerms; $acInput = $_GET[\'input\']; $mydb = new wpdb(\