Php 如何更改WooCommerce产品的产品标签列表中的排序
我使用此功能获取并显示每个产品的所有标签:Php 如何更改WooCommerce产品的产品标签列表中的排序,php,wordpress,woocommerce,product,taxonomy-terms,Php,Wordpress,Woocommerce,Product,Taxonomy Terms,我使用此功能获取并显示每个产品的所有标签: public function get_product_tags_list() { global $product; if ( ! is_a( $product, 'WC_Product' ) ) { return; } $separator = '<span class="separator"&
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 );
}
公共函数获取产品标签列表(){
全球$产品;
如果(!is_a($product,'WC_product')){
返回;
}
$separator=',;';
$before='ul> ';
$after=' ';
返回get_-term_列表($product->get_-id(),'product_-tag',$before,$separator,$after);
}
如何按slug降序排列标签列表
关于允许更改排序选项的唯一方法是基于WordPress
获取术语列表()构建您自己的函数
核心函数源代码,用获取术语()替换函数
允许其他参数更改WP\u Term\u查询
,如下所示:
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;
}
公共函数获取产品标签列表(){
全球$产品;
如果(!is_a($product,'WC_product')){
返回;
}
$taxonomy='product_tag';//分类法
$args=array(//下面是您的排序参数
'orderby'=>'slug',
“订单”=>“描述”,
);
$terms=wp\u get\u post\u terms($product->get\u id(),$taxonomy,$args);
如果(是错误($terms)){
返回$terms;
}
如果(空($条款)){
返回false;
}
$links=array();
foreach($terms作为$term){
$link=get\u term\u link($term$taxonomy);
如果(是错误($link)){
返回$link;
}
$links[]='';
}
$term_links=apply_filters(“term_links-{$taxonomy},$links);//phpcs:ignore WordPress.NamingConventions.ValidHookName.useUnderlines
$separator=',;';
$before='ul> ';
$after=' ';
返回$before.join($separator,$term_links)。$after;
}
代码进入活动子主题(或活动主题)的functions.php文件。它应该有效
文件: