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

标签 php wordpress woocommerce shortcode taxonomy

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

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

此代码经过测试并且可以工作。

关于php - 简码 - 按字母顺序将属性的所有术语显示到有序列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39489420/

相关文章:

php - 越南字符的正则表达式

php - MYSQL LIKE 类别由于 % 返回错误查询

php - Bootstrap 3 日历?

php - 使用带有 PHP 方法的 HTTPS 链接(file_get_contents、getimagesize)

php - Woocommerce 编辑结帐布局

php - Woocommerce 自定义用户输入字段

wordpress - Google Analytics by MonsterInsights WordPress 插件是否会自动插入跟踪代码,还是只是获取分析数据?

javascript - 设置无限滚动

css - WooCommerce 结帐订单审查表变化显示无

android - 签名无效 - 提供的签名与来自 Android 的 WooCommerce REST API 调用不匹配