0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
// Register the shortcode
add_shortcode('product_brand', 'wpturbo_product_brand_shortcode');
function wpturbo_product_brand_shortcode($atts) {
// Get the current post ID
$post_id = get_the_ID();
// Get the product brand term ID for the current post
$term_id = wp_get_post_terms($post_id, 'product_brand', array('fields' => 'ids'));
// Check if the product brand term ID is found
if (!empty($term_id)) {
// Get the term object
$term = get_term($term_id[0], 'product_brand');
// Get the image ACF field value for the term
$image = get_field('image', $term);
// Get the term link
$link = get_term_link($term);
// Check if the image and link are available
if ($image && $link) {
// Display the image with the term link
$output = '<a href="' . esc_url($link) . '"><img src="' . esc_url($image['url']) . '" alt="' . esc_attr($term->name) . '"></a>';
return $output;
}
}
// Return empty if no product brand is found
return '';
}
WooCommerce – Taxonomy