【问题标题】:Loop to return all Woocommerce product tags in alphabetical order循环以按字母顺序返回所有 Woocommerce 产品标签
【发布时间】:2018-12-09 17:07:30
【问题描述】:

我正在尝试在 WordPress 中为 woocommerce 主题创建一个循环,以按字母顺序列出所有产品标签 以一种简单的方式“在其下方以 A 为标题,所有标签都以 A 字母等开头”。 我正在使用代码,但它返回 null

<?php
            $tags = get_tags();
$html = '<div class="post_tags">';
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );

$html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag- 
>slug}'>";
$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;
            ?>

【问题讨论】:

    标签: php wordpress sorting woocommerce custom-taxonomy


    【解决方案1】:

    您还可以使用 WP_Term_Query 和 Woocommerce 产品标签自定义分类法,按字母顺序获取所有相关术语:

    $taxonomy  = 'product_tag';
    $tags_html = [];
    $tquery    = new WP_Term_Query( array(
        'taxonomy'     => $taxonomy,
        'orderby'      => 'name',
        'order'        => 'ASC',
        'hide_empty'   => false,
    ) );
    
    // 1st Loop: Go through each term and format it
    foreach($tquery->get_terms() as $term){
        $link   = get_term_link( $term->term_id, $taxonomy );
        $letter = $term->slug[0];
        // Set alphabetically by letter each product tag formatted html (with the class, the link and the count (optionally)
        $tags_html[$letter][] = '<a class="'.$term->slug.'" href="'.$link.'">'.$term->name.'&nbsp;('.$term->count.')'.'</a>';
    }
    
    echo '<div class="product_tags">';
    // 2nd Loop: Display all formatted product tags grouped by letter alphabetically
    foreach( $tags_html as $letter => $values ){
        echo '<div class="letter-'.$letter.'">Letter '.strtoupper($letter).':&nbsp;'.implode(' - ', $values).'</div>';
    }
    echo '</div>';
    

    经过测试和工作。


    与您的评论相关的编辑

    要限制每个字母的数字标签,您将使用:

    $taxonomy  = 'product_tag';
    $tags_html = [];
    $tquery    = new WP_Term_Query( array(
        'taxonomy'     => $taxonomy,
        'orderby'      => 'name',
        'order'        => 'ASC',
        'hide_empty'   => false,
    ) );
    
    // 1st Loop: Go through each term and format it
    foreach($tquery->get_terms() as $term){
        $link   = get_term_link( $term->term_id, $taxonomy );
        $letter = $term->slug[0];
    
        // Get the existing array keys for a letter
        $keys   = isset($tags_html[$letter]) ? array_keys($tags_html[$letter]) : array();
        // Limit to 5 items by letter
        if( sizeof($keys) < 5 ){
            // Set alphabetically by letter each product tag formatted html (with the class, the link and the count (optionally)
            $tags_html[$letter][] = '<a class="'.$term->slug.'" href="'.$link.'">'.$term->name.'&nbsp;('.$term->count.')'.'</a>';
        }
    }
    
    echo '<div class="product_tags">';
    // 2nd Loop: Display all formatted product tags grouped by letter alphabetically
    foreach( $tags_html as $letter => $values ){
        echo '<div class="letter-'.$letter.'">Letter '.strtoupper($letter).':&nbsp;'.implode(' - ', $values).'</div>';
    }
    echo '</div>';
    

    未测试...

    【讨论】:

    • 非常感谢它与我一起工作,但是当我在 agrs 中输入 'number' => '1' 时,我现在需要将每个字母的输出过滤为 1,只返回字母 A 并停止
    • @MinaAmir 我在回答结束时对此进行了更新。它未经测试。试试看,如果它不起作用,请告诉我。
    【解决方案2】:

    get_tags() 函数为post_tag 分类中的每个术语检索一组对象。 WooCommerce 产品不使用post_tag,它们使用product_tag 分类。

    你要使用的函数是get_terms():

    $terms = get_terms(
        array( 
            'hide_empty' => true,
            'taxonomy' => 'product_tag',
        ) 
    );
    $html = '<div class="post_tags">';
    if($terms){
        foreach($terms as $term){
            $term_link = get_term_link( $term->term_id, 'product_tag' );
            $html .= "<a href='" . $term_link . "' title='" . $term->name . " Tag' class='" . $term->slug . "'>" . $term->name . "</a>";
        }
    }
    $html .= "</div>";
    echo $html;
    

    如果您需要按字母顺序排列,请查看get_terms_orderby()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      • 2019-05-31
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      相关资源
      最近更新 更多