【发布时间】:2018-02-20 01:02:48
【问题描述】:
我正在做一个项目,我需要一些小组帮助。
我正在使用 woocommerce 产品系统,并且在商店存档页面产品上显示属性标签:属性值(就像文本一样)。
attribute-label:attribute-value (ex. Transmission: Manual)
有没有办法将属性标签显示为图像?我无法添加像 <img src..."/> 这样的 html 代码,并且使用 CSS 进行样式设置也无济于事。
我已经搜索过插件等等。但在网络上什么都没有。
为了在产品商店页面上显示属性,我使用了这个功能:
function isa_woocommerce_all_pa(){
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
$out = '<ul class="custom-attributes">';
foreach ( $attributes as $attribute ) {
// skip variations
if ( $attribute->get_variation() ) {
continue;
}
$name = $attribute->get_name();
if ( $attribute->is_taxonomy() ) {
$terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
// get the taxonomy
$tax = $terms[0]->taxonomy;
// get the tax object
$tax_object = get_taxonomy($tax);
// get tax label
if ( isset ( $tax_object->labels->singular_name ) ) {
$tax_label = $tax_object->labels->singular_name;
} elseif ( isset( $tax_object->label ) ) {
$tax_label = $tax_object->label;
// Trim label prefix since WC 3.0
if ( 0 === strpos( $tax_label, 'Product ' ) ) {
$tax_label = substr( $tax_label, 8 );
}
}
$out .= '<li class="' . esc_attr( $name ) . '">';
$out .= '<span class="attribute-label">' . esc_html( $tax_label ) . ': </span> ';
$out .= '<span class="attribute-value">';
$tax_terms = array();
foreach ( $terms as $term ) {
$single_term = esc_html( $term->name );
// Insert extra code here if you want to show terms as links.
array_push( $tax_terms, $single_term );
}
$out .= implode(', ', $tax_terms);
$out .= '</span></li>';
} else {
$value_string = implode( ', ', $attribute->get_options() );
$out .= '<li class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">';
$out .= '<span class="attribute-label">' . $name . ': </span> ';
$out .= '<span class="attribute-value">' . esc_html( $value_string ) . '</span></li>';
}
}
$out .= '</ul>';
echo $out;
}
add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 25);
有人可以帮我解决这个问题吗?如何用图片更改属性标签?
【问题讨论】:
-
@LoicTheAztec 也许你能帮我解决这个问题?
-
好的,我已经重新查看了您的代码并发布了答案。
标签: php css wordpress woocommerce product