这是您的全功能短代码。我用一组不同的术语对它进行了测试,它运行得很好(所以我希望你的测试是正确的,也能正常工作)。
在我迭代这个二维数组以生成您想要的显示之后
代码如下:
if (!function_exists('showbrands')) {
function showbrands(){
$term_arr = array();
$terms = get_terms( array(
'taxonomy' => 'pa_brand',
'orderby' => 'name',
'hide_empty' => false,
) );
foreach ( $terms as $term ) {
$brand = $term->name;
$slug = $term->slug;
// Getting the first letter of $brand term name
$letter = substr($brand, 0, 1);
// PREPARING DATA IN A BI DIMENSIONAL ARRAY
// Inserting the $letter in an array just once (array level 1)
// Inserting for each letter all the corresponding pairs "$brand => $slug" (array level 2)
if(!array_key_exists($letter, $term_arr))
$term_arr[$letter] = array($slug => $brand);
else
$term_arr[$letter][$slug] = $brand;
}
$output = '<div class="brandlist-container">';
// ITERATING IN THE BI DIMENTIONAL $TERM_ARR ARRAY
// first level the letters
foreach( $term_arr as $key_letter => $terms_in_letter ){
$output .= '<span id="'. strtolower( $key_letter ) .'">'. $key_letter .'</span>
<ul class="brandlist">';
// second level the $brand / $slug pairs
foreach( $terms_in_letter as $key => $value ){
$output .= '<li><a href="/brand/'.$key.'/">'.$value.'</a></li>';
}
$output .= '</ul>';
}
$output .= '</div>';
return $output;
}
add_shortcode( 'showbrands', 'showbrands' );
}
此代码继续运行。活动子主题(或主题)的php文件或任何插件文件。