Issue
I use this function to get and display all tag for each product :
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 );
}
how can I order the list of my tags by slug descending order ?
Regards
Solution
The only way to allow changing sorting options is to build your own function based on WordPress get_the_term_list()
core function source code, replacing the function get_the_terms()
by wp_get_post_terms()
which allow additional parameters to alter the WP_Term_Query
as follows:
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;
}
Code goes in functions.php file of the active child theme (or active theme). It should works.
Documentation:
- WordPress
get_the_term_list()
function. - WordPress
get_the_terms()
function. - WordPress
wp_get_post_terms()
function. - WordPress
WP_Term_Query
parameters.
Answered By - LoicTheAztec Answer Checked By - Willingham (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.