【问题标题】:Custom taxonomy grouped terms by letter alphabetically in WooCommerce自定义分类法在 WooCommerce 中按字母顺序对术语进行分组
【发布时间】:2021-06-22 21:23:39
【问题描述】:

我有 woocommerce 产品的品牌分类。我需要按品牌分类产品。
我对如何通过代码制作以下内容感兴趣:

有什么想法吗?

【问题讨论】:

    标签: php wordpress woocommerce product taxonomy-terms


    【解决方案1】:

    要按字母顺序显示链接的分类术语,您可以使用以下(在下面的代码中定义正确的自定义分类)

    $taxonomy = 'product_brand'; // <== Here define your custom taxonomy
    $sorted   = array(); // Initializing
    
    // Get all terms alphabetically sorted
    $terms = get_terms( array(
        'taxonomy'   => $taxonomy,
        'hide_empty' => true,
        'orderby'    => 'name'
    ) );
    
    // Loop through the array of WP_Term Objects
    foreach( $terms as $term ) {
        $term_name    = $term->name;
        $term_link    = get_term_link( $term, $taxonomy );
        $first_letter = strtoupper($term_name[0]);
        
        // Group terms by their first starting letter
        if( ! empty($term_link) ) {
            $sorted[$first_letter][] = '<li><a href="'.$term_link.'">'.$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">
        <h3 class="tax-letter-'.$letter.'">'.$letter.'</h3>
        <ul>' . implode('', $values) . '</ul>
        </div>';
    }
    

    它适用于任何分类法或自定义分类法(更适合非分层分类法)

    现在可以将其嵌入到简码中以便于使用:

    add_shortcode( 'terms_by_letter', 'display_terms_by_letter' );
    function display_terms_by_letter( $atts ) {
        // Shortcode Attributes
        extract( shortcode_atts( array(
            'taxonomy' => 'product_brand', // <== Here define your taxonomy
        ), $atts, 'terms_by_letter' ) );
    
        $sorted   = array(); // Initializing
        $output   = ''; // Initializing
    
        // Get all terms alphabetically sorted
        $terms = get_terms( array(
            'taxonomy'   => $taxonomy,
            'hide_empty' => true,
            'orderby'    => 'name'
        ) );
    
        // Loop through the array of WP_Term Objects
        foreach( $terms as $term ) {
            $term_name    = $term->name;
            $term_link    = get_term_link( $term, $taxonomy );
            $first_letter = strtoupper($term_name[0]);
    
            // Group terms by their first starting letter
            if( ! empty($term_link) ) {
                $sorted[$first_letter][] = '<li><a href="'.$term_link.'">'.$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 ) {
            $output .= '<div class="tax-by-letter">
            <h3 class="tax-letter-'.$letter.'">'.$letter.'</h3>
            <ul>' . implode('', $values) . '</ul>
            </div>';
        }
        return $output;
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    短代码用法:

    [terms_by_letter] 
    

    或在 PHP 代码中:

    echo do_shortcode('[terms_by_letter]');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 1970-01-01
      相关资源
      最近更新 更多