Forked from woogists/wc-display-product-attribute-archive-links.php
Last active
September 23, 2020 10:19
-
-
Save rwkyyy/e96508e80c1a5e28c011722b23c7e534 to your computer and use it in GitHub Desktop.
[General Snippets] Display product attribute in single product page with links
This file contains 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
add_action( 'woocommerce_single_product_summary', 'wc_show_attribute_links', 25 ); | |
// if you'd like to show it on archive page, replace "woocommerce_product_meta_end" with "woocommerce_shop_loop_item_title" | |
function wc_show_attribute_links() { | |
global $post; | |
$attribute_names = array( | |
'pa_###', | |
'pa_###' | |
); // 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 '<div>' . $taxonomy->labels->singular_name . ': ' . implode( $terms_array, ', ' ) . '</div>'; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment