Created
May 17, 2021 06:01
-
-
Save wbcomdev/f3b95ac83ce36afb1a3973b7fa2028fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Display product attribute archive links. | |
| */ | |
| function wb_wc_show_attribute_links() { | |
| global $post; | |
| $attribute_names = array( 'pa_color', 'pa_size' ); // Add attribute names here and remember to add the pa_ prefix to the attribute name. | |
| foreach ( $attribute_names as $attribute_name ) { | |
| $taxonomy = get_taxonomy( $attribute_name ); | |
| if ( $taxonomy && ! is_wp_error( $taxonomy ) ) { | |
| $terms = wp_get_post_terms( $post->ID, $attribute_name ); | |
| $terms_array = array(); | |
| if ( ! empty( $terms ) ) { | |
| foreach ( $terms as $term ) { | |
| $archive_link = get_term_link( $term->slug, $attribute_name ); | |
| $full_line = '<a href="' . $archive_link . '">' . $term->name . '</a>'; | |
| array_push( $terms_array, $full_line ); | |
| } | |
| echo $taxonomy->labels->name . ' ' . implode( $terms_array, ', ' ); | |
| } | |
| } | |
| } | |
| } | |
| add_action( 'woocommerce_product_meta_end', 'wb_wc_show_attribute_links' ); | |
| // if you'd like to show it on archive page, replace "woocommerce_product_meta_end" with "woocommerce_shop_loop_item_title". |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment