要隐藏 WooCommerce 产品名称(或标题),请使用以下命令:
add_filter( 'the_title', 'hide_product_title', 10, 2 );
function hide_product_title( $title, $post_id ) {
global $woocommerce_loop;
if ( ! is_admin() && ! empty($woocommerce_loop) ) {
$title = '';
}
return $title;
}
add_filter( 'woocommerce_product_get_name', 'hide_product_name', 10, 2 );
add_filter( 'woocommerce_product_variation_get_name', 'hide_product_name', 10, 2 );
function hide_product_name( $name, $product ) {
if ( ! is_admin() ) {
$name = '';
}
return $name;
}
现在要在任何地方隐藏作为产品标题包含的域名,请使用以下(将域名替换为星号,首尾字符除外): p>
// Custom function to replace a string (domain name) with a repeating character (a star by default)
function hide_domain_name( $string, $repl_char = '*' ) {
$index_needle = strpos($string, '.');
$replacement = str_repeat($repl_char, ($index_needle > 2 ? $index_needle - 2 : strlen($string) - 1));
return substr_replace($string, $replacement, 1) . substr($string, ($index_needle > 2 ? $index_needle - 1 : strlen($string) - 1));
}
add_filter( 'the_title', 'hide_product_title', 10, 2 );
function hide_product_title( $title; $post_id ) {
global $woocommerce_loop;
if ( ! is_admin() && ! empty($woocommerce_loop) ) {
$title = hide_domain_name( $title );
}
return $title;
}
add_filter( 'woocommerce_product_get_name', 'hide_product_name', 10, 2 );
add_filter( 'woocommerce_product_variation_get_name', 'hide_product_name', 10, 2 );
function hide_product_name( $name, $product ) {
if ( ! is_admin() ) {
$name = hide_domain_name( $name );
}
return $name;
}
不要忘记为您的产品制作自定义永久链接,因为产品标题(域名)应该出现在其中。
代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。