Reduce product long description in Woocommerce

那年仲夏 提交于 2019-12-02 01:56:47

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!