I am trying to place a clickable link for brands under the product title on Woocommerce single product pages.
I have successfully been able to put the brand under the title, but can't make it a link to the brand category page.
Any help?
I'm using the WooCommerce Brands plugin.
And here is the code I'm using:
add_action( 'woocommerce_single_product_summary', 'wc_brands_add_brand_name', 8 );
function wc_brands_add_brand_name() {
global $product;
$brands = implode(', ', wp_get_post_terms($product->get_id(), 'product_brand', ['fields' => 'names']));
echo "<p>Brand: " . '<a href="' . 'need hyperlink here' . '">' . $brands . '</a>' . "</p>";
}
CodePudding user response:
get_the_term_list is the right function to use in a situation where you want to show links of terms of a post.
In get_the_term_list
you can set before, after & separator, so you don't even have to make an if
check for show/hide the string.
Here is the code:
/**
* Display product brands
*/
function vh_wc_display_brand_links() {
global $product;
echo get_the_term_list( $product->get_id(), 'product_brand', '<p >' . __( 'Brands:', 'text-domain' ) . ' ', ', ', '</p>' );
}
add_action( 'woocommerce_single_product_summary', 'vh_wc_display_brand_links', 8 );