我想使用<
ul>
<
li>
代替<
select>
<
option>
我在function.php中输入此代码, 但不起作用:
function wc_dropdown_variation_attribute_options( $args = array() ) {
.
.
.
$html= '<
div class="tt-wrapper">
<
div class="tt-title-options">
??????:<
/div>
<
ul id="' . esc_attr( $id ) . '" class="tt-options-swatch options-large ' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">
';
if ( ! empty( $options ) ) {
if ( $product &
&
taxonomy_exists( $attribute ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms(
$product->
get_id(), $attribute, array(
'fields' =>
'all', )
);
foreach ( $terms as $term ) {
if ( in_array( $term->
slug, $options, true ) ) {
$html .= '<
li>
<
a href="http://www.srcmini.com/#" value="' . esc_attr( $term->
slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->
slug, false ) . '>
' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->
name, $term, $attribute, $product ) ) . '<
/a>
<
/li>
';
}
}
} else {
foreach ( $options as $option ) {
// This handles <
2.4.0 bw compatibility where text attributes were not sanitized.
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
$html.= '<
li>
<
a href="http://www.srcmini.com/#" value="' . esc_attr( $option ) . '" ' . $selected . '>
' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option, null, $attribute, $product ) ) . '<
/a>
<
/li>
';
}
}
}$html .= '<
/ul>
<
/div>
<
/div>
';
echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args );
// WPCS: XSS ok.
}
如何将woocommerce HTML代码更改为自己的HTML代码?
这是发布此问题的更多详细信息
#1要覆盖WooCommerce的默认主题, 最好在主题文件夹中有一个” woocommerce” 子目录。例如:/ wp-content / themes / mytheme / woocommerce /你可以在其中精确指定要更改的主题。点击此处了解详情。
【如何在WooCommerce中更改” 添加到购物车” ()】另外, 你可以挂钩到动作和过滤器。在你发送的示例中, 你可以连接到以下过滤器:woocommerce_dropdown_variation_attribute_options_html更改其最终输出。在这里和这里了解更多有关它的信息。
#2
function woocommerce_dropdown_variation_attribute_options_html_callback( $html, $args ) {
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute'];
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
// We'll do our best to hide the placeholder, but we'll need to show something when resetting options. if ( empty( $options ) &
&
! empty( $product ) &
&
! empty( $attribute ) ) {
$attributes = $product->
get_variation_attributes();
$options = $attributes[ $attribute ];
}$html = '<
ul id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">
';
$html .= '<
li value="">
' . esc_html( $show_option_none_text ) . '<
/li>
';
if ( ! empty( $options ) ) {
if ( $product &
&
taxonomy_exists( $attribute ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms( $product->
get_id(), $attribute, array( 'fields' =>
'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->
slug, $options ) ) {
$html .= '<
li value="' . esc_attr( $term->
slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->
slug, false ) . '>
' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->
name ) ) . '<
/li>
';
}
}
} else {
foreach ( $options as $option ) {
// This handles <
2.4.0 bw compatibility where text attributes were not sanitized.
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
$html .= '<
li value="' . esc_attr( $option ) . '" ' . $selected . '>
' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '<
/li>
';
}
}
}
$html .= '<
/ul>
';
return $html;
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'woocommerce_dropdown_variation_attribute_options_html_callback', 10, 2 );
推荐阅读
- 如何更改WordPress主题的所有按钮,图标和其他元素颜色()
- 如何仅在台式机/笔记本电脑的Rehub主题中居中主菜单()
- 如何从自定义页面模板调用WordPress插件函数()
- 如何使用具有Google Maps集成的jaredpalmer/presspack构建主题()
- 如何在WordPress中阻止自定义帖子类型列表页面()
- 如何在DIVI代码模块中应用样式()
- 如何在wpbakery行中对齐小部件中心()
- 如何将主题添加到现有的实时网站
- MySQL数据库常见操作命令