循环以字母顺序返回所有Woocommerce产品标签

时间:2018-07-01 01:40:28

标签: php wordpress sorting woocommerce custom-taxonomy

我正尝试在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;
            ?>

2 个答案:

答案 0 :(得分: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>';

未经测试...

答案 1 :(得分:0)

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()