您可以有条件地使用is_page
因此woo_custom_order_button_text
函数仅针对指定的任何页面返回:
// When any single Page is being displayed.
is_page();
// When Page 42 (ID) is being displayed.
is_page( 42 );
// When the Page with a post_title of "Contact" is being displayed.
is_page( \'Contact\' );
// When the Page with a post_name (slug) of "about-me" is being displayed.
is_page( \'about-me\' );
/*
* Returns true when the Pages displayed is either post ID 42,
* or post_name "about-me", or post_title "Contact".
* Note: the array ability was added in version 2.5.
*/
is_page( array( 42, \'about-me\', \'Contact\' ) );
有不同的方法可以应用此功能。如果您想将其限制在产品和页面上,我可以尝试这样的方法
checkout
是您只希望过滤器运行的页面:
if( is_page( \'checkout\' ) ) {
add_filter( \'woocommerce_product_single_add_to_cart_text\', \'woo_custom_cart_button_text\' );
add_filter( \'woocommerce_order_button_text\', \'woo_custom_order_button_text\' );
}
function woo_custom_cart_button_text( $text ) {
global $product;
if ( 123 === $product->id ) {
$text = \'Product 123 text\';
}
return $text;
}
function woo_custom_order_button_text() {
return __( \'Your new button text here\', \'woocommerce\' );
}