按字母顺序循环 return 所有 Woocommerce 产品标签
Loop to return all Woocommerce product tags in alphabetical order
我正在尝试在 WordPress 中为 woocommerce 主题创建一个循环,以按字母顺序取出所有产品标签
以简单的方式 "heading with A below it all tags starts with A letter etc."
我正在使用代码,但它的 return 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;
?>
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()
您还可以将 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.' ('.$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).': '.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.' ('.$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).': '.implode(' - ', $values).'</div>';
}
echo '</div>';
未测试…
我正在尝试在 WordPress 中为 woocommerce 主题创建一个循环,以按字母顺序取出所有产品标签 以简单的方式 "heading with A below it all tags starts with A letter etc." 我正在使用代码,但它的 return 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;
?>
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()
您还可以将 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.' ('.$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).': '.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.' ('.$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).': '.implode(' - ', $values).'</div>';
}
echo '</div>';
未测试…