【问题标题】:How to change sorting in a list of product tags for WooCommerce products如何更改 WooCommerce 产品的产品标签列表中的排序
【发布时间】:2023-03-17 19:44:01
【问题描述】:

我使用这个函数来获取和显示每个产品的所有标签:

public function get_product_tags_list() {
            global $product;

            if ( ! is_a( $product, 'WC_Product' ) ) {
                return;
            }

            $separator = '<span class="separator">&#44;&nbsp;</span></li><li>';
            $before    = '<ul><li>';
            $after     = '</li></ul>';

            return get_the_term_list( $product->get_id(), 'product_tag', $before, $separator, $after );
        }

如何按 slug 降序排列我的标签列表?

问候

【问题讨论】:

    标签: php wordpress woocommerce product taxonomy-terms


    【解决方案1】:

    允许更改排序选项的唯一方法是基于 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">&#44;&nbsp;</span></li><li>';
        $before    = '<ul><li>';
        $after     = '</li></ul>';
        
        return $before . join( $separator, $term_links ) . $after;
    }
    

    代码进入活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。


    文档:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      • 2017-03-31
      相关资源
      最近更新 更多