我已经创建了一个自定义字段,用于在woocommerce中为品牌添加品牌徽标图片。在前端,徽标显示在产品摘要的开头 - 产品标题上方,带有以下代码:
/**Add MFG Logo Above Short Description*/
add_action( 'woocommerce_single_product_summary', 'brands');
function brands() {
global $post;
global $product;
$mfglogo= get_post_meta($post->ID, 'brand_logo', true);
echo '<div>';
echo '<img src="' . $mfglogo.'"></a>';
echo'</div>';
}
我想将此图片链接到相应的品牌档案页面。我也为品牌创建了一个全局属性,因此我可以按品牌过滤产品页面。
我了解到你可以将你的属性放到前端的列表中,并且可以使用以下代码点击它们的属性存档页面:
/**Add Brand Link to META List*/
function list_attributes( $product ) {
global $product;
global $post;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
foreach ( $attributes as $attribute ) {
// Get the taxonomy.
$terms = wp_get_post_terms( $product->id, $attribute[ 'name' ], 'all' );
$taxonomy = $terms[ 0 ]->taxonomy;
// Get the taxonomy object.
$taxonomy_object = get_taxonomy( $taxonomy );
// Get the attribute label.
$attribute_label = $taxonomy_object->labels->name;
// Display the label followed by a clickable list of terms.
echo get_the_term_list( $post->ID, $attribute[ 'name' ] , '<div class="attributes">' . $attribute_label . ': ' , ', ', '</div>' );
break;
}
}
add_action( 'woocommerce_product_meta_end', 'list_attributes' );
。我如何使用这个想法使徽标图像可链接???
答案 0 :(得分:0)
您可以替换它:
echo '<div>';
echo '<a href="' . $attribute. '"><img src="' . $download4 .'"></a>';
echo'</div>';
以下内容:
foreach ( $attribute as $attr) {
echo '<div>';
echo '<a href="#' . $attr->get_name(). '"><img src="' . $download4 .'"></a>';
echo'</div>';
// Uncomment the follwing line if you want to display the first attribute only (makes sense since you only have one logo image)
// break;
}