WordPress自定义分类autorzy按姓氏首字母分组排序异常问题
解决WordPress自定义分类作者按姓氏首字母A-Z分组排序问题
我在WordPress中创建了用于展示作者的自定义分类“autorzy”,并添加了“Last Name”字段(字段名nazwisko)以按姓氏对作者排序,作者按姓氏首字母分组显示。但当前分组显示顺序为Z、R、P、C,不符合预期的A、B、C字母顺序,相关实现代码如下:
<?php $taxonomy = 'autorzy'; // <== Here define your custom taxonomy // Get all terms alphabetically sorted $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'posts_per_page' => -1, 'orderby' => $term_last_name, 'order' => 'DESC', ) ); $sorted = array(); // Initializing // Loop through the array of WP_Term Objects foreach( $terms as $term ) { $term_name = $term->name; $term_last_name = get_field('nazwisko', $term); $image = get_field('obrazek_wyrozniajacy', $term); $term_link = get_term_link( $term, $taxonomy ); $first_letter = strtoupper($term_last_name[0]); // Group terms by their first starting letter if( ! empty($term_link) ) { $sorted[$first_letter][] = '<li><a href="'.$term_link.'">' .'<img src="'.$image['url'].'" />'.$term_name.'</a></li>'; } else { $sorted[$first_letter][] = '<li>'.$term_name.'</li>'; } } // Loop through grouped terms by letter to display them by letter foreach( $sorted as $letter => $values ) { echo '<div class="tax-by-letter" id="tax-letter-'.$letter.'"> <h3 class="tax-letter-'.$letter.'">'.$letter.'</h3> <ul class="nav">' . implode('', $values) . '</ul> </div>'; } ?>
解决方案
需要对代码做两处关键修改,确保分组按A-Z顺序展示:
修正
get_terms的排序逻辑- 原代码中
orderby使用了未定义的变量$term_last_name,无法正确按姓氏排序;同时order设为DESC会导致反向排序。 - 由于
nazwisko是ACF自定义字段,需要通过meta_key指定字段名,并用meta_value作为排序依据,同时将order改为ASC实现升序。
- 原代码中
对分组后的首字母数组排序
- 即使获取到的terms是升序,分组后的
sorted数组的键(首字母)不一定自动按A-Z排列,需要用ksort()对数组按键排序。
- 即使获取到的terms是升序,分组后的
修改后的完整代码
<?php $taxonomy = 'autorzy'; // <== Here define your custom taxonomy // Get all terms sorted by last name in ascending order $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'posts_per_page' => -1, 'meta_key' => 'nazwisko', // 指定ACF字段名 'orderby' => 'meta_value', // 按自定义字段值排序 'order' => 'ASC', // 升序排列(A-Z) ) ); $sorted = array(); // Initializing // Loop through the array of WP_Term Objects foreach( $terms as $term ) { $term_name = $term->name; $term_last_name = get_field('nazwisko', $term); $image = get_field('obrazek_wyrozniajacy', $term); $term_link = get_term_link( $term, $taxonomy ); $first_letter = strtoupper($term_last_name[0]); // Group terms by their first starting letter if( ! empty($term_link) ) { $sorted[$first_letter][] = '<li><a href="'.$term_link.'">' .'<img src="'.$image['url'].'" />'.$term_name.'</a></li>'; } else { $sorted[$first_letter][] = '<li>'.$term_name.'</li>'; } } // 对首字母分组进行A-Z排序 ksort($sorted); // Loop through grouped terms by letter to display them by letter foreach( $sorted as $letter => $values ) { echo '<div class="tax-by-letter" id="tax-letter-'.$letter.'"> <h3 class="tax-letter-'.$letter.'">'.$letter.'</h3> <ul class="nav">' . implode('', $values) . '</ul> </div>'; } ?>
关键修改说明
get_terms参数中添加了meta_key和修正了orderby、order,确保terms先按姓氏升序获取。- 在输出分组前调用
ksort($sorted),强制首字母分组按A-Z的顺序排列。
内容的提问来源于stack exchange,提问作者Patryk Banach
相关产品推荐
相关产品推荐

