我试图有条件地改变WooCommerce我的帐户端点上的字符串。每次我尝试添加端点条件时,它要么破坏网站,要么不起作用;然而,当我不使用条件字符串替换工作。也许有人能指出我错在哪里。
工作代码段-无条件
function change_endpoint_text( $translated ) {
$translated = str_ireplace( 'List of coupons which are valid & available for use. Click on the coupon to use it. The coupon discount will be visible only when at least one product is present in the cart.', 'List of vouchers which are valid & available for use. Click on a voucher to use it. The discount will be visible only when at least one product is present in the cart.', $translated );
return $translated;
}
add_filter( 'gettext', 'change_endpoint_text' );
条件代码段测试-不工作
注意:我也尝试使用is_account_page条件,似乎打破了网站。
function change_endpoint_text( $translated ) {
if( is_wc_endpoint_url('wc-smart-coupons') ) {
$translated = str_ireplace( 'List of coupons which are valid & available for use. Click on the coupon to use it. The coupon discount will be visible only when at least one product is present in the cart.', 'List of vouchers which are valid & available for use. Click on a voucher to use it. The discount will be visible only when at least one product is present in the cart.', $translated );
}
return $translated;
}
add_filter( 'gettext', 'change_endpoint_text' );
如有任何帮助,不胜感激。
您也可以使用$wp->query_vars['custom_endpoint']
检查终点。
例如,
function change_endpoint_text ( $translated_text, $text, $domain ) {
global $wp;
if ( isset( $wp->query_vars['wc-smart-coupons'] ) ) {
if ($translated_text == 'some existing text') {
$translated_text = __( 'The new replaced text', 'woocommerce');
}
}
return $translated_text;
}
add_filter( 'gettext', 'change_endpoint_text' );