从 WooCommerce 产品标题中删除第一个词
Remove first word from the WooCommerce product title
是否可以在 WooCommerce 中删除产品标题的第一个词?我找到了一些 php 代码,但我现在无法理解。
echo substr(strstr("Remove First Word"," "), 1);
那应该回显 "First Word"。我将如何为 WooCommerce 产品标题做那件事?感谢所有帮助!
使用这个:
$str = "Remove First Word";
$words = explode(' ', $str);
unset($words[0]);
echo join(' ', $words);
explode
函数 returns 一个包含每个单词的数组。
unset
函数删除数组$words
中包含的第一个单词。
最后,join
打印由 space </code>.</p> 加入的所有 <code>$words
你能试试用这个吗?
if ( ! function_exists( 'woocommerce_template_loop_product_title' ) ) {
/**
* Removes first word in WooCommerce product_title
* @var $tag
*/
function woocommerce_template_loop_product_title() {
$tag = is_product_taxonomy() || is_shop() ? 'h2' : 'h3';
echo apply_filters( 'woocommerce_template_loop_product_title', '<' . $tag . ' class="woocommerce-loop-product__title">' . substr(strstr(get_the_title()," "), 1) . '</' . $tag . '>');
}
/**
* Removes first word in WooCommerce product page product_title
* @var $tag
*/
function woocommerce_single_product_summary() {
$tag = 'h1';
echo apply_filters(woocommerce_single_product_summary, '<' . $tag . ' class="product_title entry-title">' . substr(strstr(get_the_title()," "), 1) . '</' . $tag . '>');
}
}
希望这对你有用,还没有测试过。
对于单个产品页面和存档页面中的产品标题:
add_filter( 'the_title', 'custom_the_title', 10, 2 );
function custom_the_title( $title, $post_id ){
$post_type = get_post_field( 'post_type', $post_id, true );
if( $post_type == 'product' || $post_type == 'product_variation' )
$title = substr( strstr( $title, ' ' ), 1 );
return $title;
}
代码进入您的活动 child 主题(或活动主题)的 function.php 文件。
已测试并有效。
The product title uses WordPress function get_the_title()
or the_title()
to be displayed (as woocommerce product is a custom post type)… so the correct filter hook to be used is "the_title"
.
但它不会真正处理 html 标签(因为这是模板中的其他内容)。
对于购物车和结帐页面:
add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_name', 10, 3);
function customizing_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
$product_permalink = $product->is_visible() ? $product->get_permalink( $cart_item ) : '';
$product_name = $product->get_name();
$product_name = substr( strstr( $product_name, ' ' ), 1 );
if ( $product_permalink && is_cart() ) {
return sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $product_name );
} elseif ( ! $product_permalink && is_cart() ) {
return $product_name . ' ';
} else {
return $product_name;
}
}
代码进入您的活动 child 主题(或活动主题)的 function.php 文件。
已测试并有效。
是否可以在 WooCommerce 中删除产品标题的第一个词?我找到了一些 php 代码,但我现在无法理解。
echo substr(strstr("Remove First Word"," "), 1);
那应该回显 "First Word"。我将如何为 WooCommerce 产品标题做那件事?感谢所有帮助!
使用这个:
$str = "Remove First Word";
$words = explode(' ', $str);
unset($words[0]);
echo join(' ', $words);
explode
函数 returns 一个包含每个单词的数组。
unset
函数删除数组$words
中包含的第一个单词。
最后,join
打印由 space </code>.</p> 加入的所有 <code>$words
你能试试用这个吗?
if ( ! function_exists( 'woocommerce_template_loop_product_title' ) ) {
/**
* Removes first word in WooCommerce product_title
* @var $tag
*/
function woocommerce_template_loop_product_title() {
$tag = is_product_taxonomy() || is_shop() ? 'h2' : 'h3';
echo apply_filters( 'woocommerce_template_loop_product_title', '<' . $tag . ' class="woocommerce-loop-product__title">' . substr(strstr(get_the_title()," "), 1) . '</' . $tag . '>');
}
/**
* Removes first word in WooCommerce product page product_title
* @var $tag
*/
function woocommerce_single_product_summary() {
$tag = 'h1';
echo apply_filters(woocommerce_single_product_summary, '<' . $tag . ' class="product_title entry-title">' . substr(strstr(get_the_title()," "), 1) . '</' . $tag . '>');
}
}
希望这对你有用,还没有测试过。
对于单个产品页面和存档页面中的产品标题:
add_filter( 'the_title', 'custom_the_title', 10, 2 );
function custom_the_title( $title, $post_id ){
$post_type = get_post_field( 'post_type', $post_id, true );
if( $post_type == 'product' || $post_type == 'product_variation' )
$title = substr( strstr( $title, ' ' ), 1 );
return $title;
}
代码进入您的活动 child 主题(或活动主题)的 function.php 文件。
已测试并有效。
The product title uses WordPress function
get_the_title()
orthe_title()
to be displayed (as woocommerce product is a custom post type)… so the correct filter hook to be used is"the_title"
.
但它不会真正处理 html 标签(因为这是模板中的其他内容)。
对于购物车和结帐页面:
add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_name', 10, 3);
function customizing_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
$product_permalink = $product->is_visible() ? $product->get_permalink( $cart_item ) : '';
$product_name = $product->get_name();
$product_name = substr( strstr( $product_name, ' ' ), 1 );
if ( $product_permalink && is_cart() ) {
return sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $product_name );
} elseif ( ! $product_permalink && is_cart() ) {
return $product_name . ' ';
} else {
return $product_name;
}
}
代码进入您的活动 child 主题(或活动主题)的 function.php 文件。
已测试并有效。