代码之家  ›  专栏  ›  技术社区  ›  d1ch0t0my

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

  •  2
  • d1ch0t0my  · 技术社区  · 8 年前

    我创建了一个简单的短代码,用于输出属性术语并链接到它们各自的存档。我希望能够按字母分割结果,以便它们输出如下:

    A
    Adidas
    Askwith
    
    B
    Bonham
    Burberry
    

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

    我如何做到这一点?

    谢谢

    1 回复  |  直到 8 年前
        1
  •  3
  •   Community Neeleshkumar S    4 年前

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

    在我迭代这个二维数组以生成您想要的显示之后

    代码如下:

    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文件或任何插件文件。