在特定的 WooCommerce 结帐字段包装器 html 标签中放置一个 span 标签
Put a span tag inside specific WooCommerce checkout field wrapper html tag
我正在尝试将 <span>
标签放入结帐页面的 <p>
包装器标签中以用于运送城市字段。我尝试了很多不同的方法,但都失败了。
这是我的代码尝试(不起作用):
function hackies( $field, $key, $args, $value ) {
// Wrap all fields except first and last name.
if ( $key === 'shipping_city' ) {
$field .= '<span>hello world</span>';
}
return $field;
}
add_filter( 'woocommerce_form_field_text', 'hackies', 10, 4);
但是它不起作用,有人可以帮忙吗?
你没有使用正确的钩子和正确的方法。在钩子函数中使用 PHP str_replace()
函数,使用 woocommerce_form_field
过滤器钩子完成工作:
add_filter( 'woocommerce_form_field', 'hackies', 10, 4);
function hackies( $field, $key, $args, $value ) {
// Wrap all fields except first and last name.
if ( $key === 'shipping_city' ) {
$field = str_replace( array('<label ', '</span>'), array('<span class="special"><label ', '</span></span>'), $field );
}
return $field;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
This works for "input text" fields type… It requires something a bit different for other fields types.
我正在尝试将 <span>
标签放入结帐页面的 <p>
包装器标签中以用于运送城市字段。我尝试了很多不同的方法,但都失败了。
这是我的代码尝试(不起作用):
function hackies( $field, $key, $args, $value ) {
// Wrap all fields except first and last name.
if ( $key === 'shipping_city' ) {
$field .= '<span>hello world</span>';
}
return $field;
}
add_filter( 'woocommerce_form_field_text', 'hackies', 10, 4);
但是它不起作用,有人可以帮忙吗?
你没有使用正确的钩子和正确的方法。在钩子函数中使用 PHP str_replace()
函数,使用 woocommerce_form_field
过滤器钩子完成工作:
add_filter( 'woocommerce_form_field', 'hackies', 10, 4);
function hackies( $field, $key, $args, $value ) {
// Wrap all fields except first and last name.
if ( $key === 'shipping_city' ) {
$field = str_replace( array('<label ', '</span>'), array('<span class="special"><label ', '</span></span>'), $field );
}
return $field;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
This works for "input text" fields type… It requires something a bit different for other fields types.