从WooCommerce产品标题中删除第一个单词

时间:2018-02-08 13:01:11

标签: php wordpress woocommerce product hook-woocommerce

是否可以从WooCommerce的产品标题中删除第一个单词?我发现了一些PHP代码,但我现在无法弄明白。

echo substr(strstr("Remove First Word"," "), 1);

那应该回应“第一个字”。我如何为WooCommerce产品标题做到这一点?我感谢所有的帮助!

3 个答案:

答案 0 :(得分:1)

使用它:

  $str = "Remove First Word";
  $words = explode(' ', $str);
  unset($words[0]);
  echo join(' ', $words);

explode函数返回一个包含每个单词的数组。

unset函数删除数组$words中包含的第一个单词。

最后,join打印按空格$words加入的所有

demo

答案 1 :(得分:0)

你可以尝试使用它吗?

glsl 1 took 394.147992134094ms
glsl 2 took 94.601035118103ms
glsl 3 took 81.4379453659058ms
glsl 4 took 76.9931077957153ms
glsl 5 took 77.0320892333984ms
glsl 6 took 75.8579969406128ms
glsl 7 took 76.9950151443481ms
glsl 8 took 77.8199434280396ms
glsl 9 took 79.7009468078613ms
glsl 10 took 79.4800519943237ms
metal 1 took 146.992921829224ms
metal 2 took 88.6669158935547ms
metal 3 took 81.8150043487549ms
metal 4 took 78.1329870223999ms
metal 5 took 79.5910358428955ms
metal 6 took 93.6589241027832ms
metal 7 took 94.8940515518188ms
metal 8 took 89.0530347824097ms
metal 9 took 84.3830108642578ms
metal 10 took 77.949047088623ms

希望这对你有用,没有测试过。

答案 2 :(得分:0)

对于单个产品页面和存档页面中的产品标题:

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;
}

代码进入活动子主题(或活动主题)的function.php文件。

经过测试和工作。

  

产品标题使用WordPress功能get_the_title()the_title()显示(因为woocommerce产品是自定义帖子类型)...因此要使用的正确过滤器挂钩是"the_title"。< / p>

但它不能真正处理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 . '&nbsp;';
    } else {
        return $product_name;
    }
}

代码进入活动子主题(或活动主题)的function.php文件。

经过测试和工作。