更改标签文本(仅限文本)
文本“会员折扣!”可以使用翻译过滤器进行更改,但无法向其中添加图像或任何HTML,因为稍后在问题中引用的代码中会对其进行转义:
$badge = \'<span class="onsale wc-memberships-member-discount">\' . esc_html( $label ) . \'</span>\';
下面是一个示例,演示如何更改会员折扣文本,但它仅适用于文本,而不适用于HTML:
add_filter( \'gettext\', \'wpse244685_change_text\', 20, 3 );
function wpse244685_change_text( $translated_text, $untranslated_text, $domain ) {
if ( \'woocommerce-memberships\' !== $domain ) {
return $translated_text;
}
// make the changes to the text
switch( $untranslated_text ) {
case \'Member discount!\' :
// $translated_text = __( \'New text\', \'text_domain\' ); // Example of new string
$translated_text = \'\'; // Empty strings should not be translated
break;
// add more items
}
return $translated_text;
}
添加图像或其他HTML取决于产品的配置方式,您可以通过
wc_memberships_member_discount_badge
或
wc_memberships_variation_member_discount_badge
过滤器如下所示:
Standard Product:
add_filter( \'wc_memberships_member_discount_badge\', \'wpse244685_wc_memberships_member_discount_badge\', 10, 3 );
function wpse244685_wc_memberships_member_discount_badge( $badge, $the_post, $product ) {
$badge = \'<span class="onsale wc-memberships-member-discount">\' .
\'<img src="https://placekitten.com/300/300" alt="meow">\' .
\'</span>\';
return $badge;
}
Variable Product:
add_filter( \'wc_memberships_variation_member_discount_badge\', \'wpse244685_wc_memberships_variation_member_discount_badge\', 10, 2 );
function wpse244685_wc_memberships_member_discount_badge( $badge, $product ) {
$badge = \'<span class="onsale wc-memberships-member-discount">\' .
\'<img src="https://placekitten.com/300/300" alt="meow">\' .
\'</span>\';
return $badge;
}