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