减少 Woocommerce 中的产品长描述 [英] Reduce product long description in Woocommerce

查看:47
本文介绍了减少 Woocommerce 中的产品长描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现以下代码

之后:

类似:限制 Woocommerce 中的产品简短描述长度

I have found the following code from this answer thread, but it only applied under the product title. So how can apply to the detailed description of the product.

add_action( 'woocommerce_after_shop_loop_item_title', 'shorten_product_excerpt', 35 );
function shorten_product_excerpt()
{
    global $post;
    $limit = 14;
    $text = $post->post_excerpt;
    if (str_word_count($text, 0) > $limit) {
        $arr = str_word_count($text, 2);
        $pos = array_keys($arr);
        $text = substr($text, 0, $pos[$limit]) . '...';
        // $text = force_balance_tags($text); // may be you dont need this…
    }
    echo '<span class="excerpt"><p>' . $text . '</p></span>';
}

解决方案

In Woocommerce product single pages, the long description is displayed in the "description tab". If you look at the source code of single-product/tabs/description.php template, it use the_content() wordpress function to display that long description.

So you can use the_content dedicated Wordpress filter hook to reduce the product long description:

add_filter( 'the_content', 'shorten_product_long_descrition', 20 );
function shorten_product_long_descrition( $content ){
    // Only for single product pages
    if( ! is_product() ) return $content;

    // Set the limit of words
    $limit = 14;

    if (str_word_count($content, 0) > $limit) {
        $arr = str_word_count($content, 2);
        $pos = array_keys($arr);
        $text = '<p>' . substr($content, 0, $pos[$limit]) . '...</p>';
        $content = force_balance_tags($text); // needded
    }
    return $content;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Before:

After:

Similar: Limit product short description length in Woocommerce

这篇关于减少 Woocommerce 中的产品长描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆