简码 - 按字母顺序将属性的所有术语显示到有序列表中

Shortcode - Display all terms of an attribute into an ordered list by alphabetical letters

我创建了一个简单的短代码来输出属性词和 link 到它们各自的档案。我希望能够按字母拆分结果,以便它们输出如下:

A
Adidas
Askwith

B
Bonham
Burberry

...在 HTML 中看起来像这样:

<span id="a">A</span>
<ul class="brandlist">
<li><a href="/brand/adidas">Adidas</a></li>
<li><a href="/brand/askwith">Askwith</a></li>
</ul>

<span id="b">B</span>
<ul class="brandlist">
<li><a href="/brand/bonham">Bonham</a></li>
<li><a href="/brand/burberry">Burberry</a></li>
</ul>

但我对如何执行此操作有点不知所措。我读过几篇建议为字母创建新的自定义分类法的文章,但要完成一些我希望能以更简单的方式解决的事情,这似乎还有很长的路要走。

这是我目前创建的简码:

function brands_output( $atts ){

    ob_start();
    echo '<ul class="brandlist">';

    $terms = get_terms( array(
        'taxonomy' => 'pa_brand',
        'orderby' => 'name',
        'hide_empty' => false,
        )
    );

    foreach ( $terms as $term ) {
    $brand = $term->name;
    $slug = $term->slug;

    echo '<li><a href="/brand/'.$slug.'/">'.$brand.'</a></li>';
    }

    echo '</ul>';
    $output = ob_get_clean();
    return $output;

}
add_shortcode( 'showbrands', 'brands_output' );

我如何做到这一点?

谢谢

这是您的全功能短代码。我用一组不同的术语对其进行了测试,它运行良好(所以我希望你的是正确的并且也能正常工作)。

First I prepare the data in a bi-dimentional array with the alphabetical letters on level 1 and the corresponding terms pair name/slug on level2.

After I iterate in this bi-dimentional array to make the display you want to have…

代码如下:

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' );
    
}

此代码在您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码已经过测试并且有效。