我使用此函数来获取并显示每个产品的所有标签:
public function get_product_tags_list() {
global $product;
if ( ! is_a( $product, 'WC_Product' ) ) {
return;
}
$separator = '<span class="separator">, </span></li><li>';
$before = '<ul><li>';
$after = '</li></ul>';
return get_the_term_list( $product->get_id(), 'product_tag', $before, $separator, $after );
}
如何按 slug 降序对标签列表进行排序?
问候
最佳答案
允许更改排序选项的唯一方法是基于 WordPress get_the_term_list()
核心函数源代码构建您自己的函数,将函数 get_the_terms()
替换为 wp_get_post_terms()
允许附加参数更改 WP_Term_Query
,如下所示:
public function get_product_tags_list() {
global $product;
if ( ! is_a( $product, 'WC_Product' ) ) {
return;
}
$taxonomy = 'product_tag'; // The taxonomy
$args = array( // Your sorting parameters below
'orderby' => 'slug',
'order' => 'DESC',
);
$terms = wp_get_post_terms( $product->get_id(), $taxonomy, $args );
if ( is_wp_error( $terms ) ) {
return $terms;
}
if ( empty( $terms ) ) {
return false;
}
$links = array();
foreach ( $terms as $term ) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) ) {
return $link;
}
$links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
}
$term_links = apply_filters( "term_links-{$taxonomy}", $links ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
$separator = '<span class="separator">, </span></li><li>';
$before = '<ul><li>';
$after = '</li></ul>';
return $before . join( $separator, $term_links ) . $after;
}
代码位于事件子主题(或事件主题)的functions.php 文件中。它应该有效。
文档:
关于php - 如何更改 WooCommerce 产品的产品标签列表中的排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65005503/