ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php-在Woocommerce中减少产品的详细描述

2019-10-12 05:42:37  阅读:248  来源: 互联网

标签:php wordpress woocommerce product hook-wordpress


我发现以下代码from this answer thread,但它仅在产品标题下适用.那么如何适用于该产品的详细说明.

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

解决方法:

在Woocommerce产品单页中,详细描述显示在“描述”选项卡中.如果您查看single-product / tabs / description.php模板的源代码,它将使用the_content()wordpress函数显示该详细说明.

因此,您可以使用the_content专用的Wordpress过滤器挂钩来减少产品的详细说明:

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

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试和工作.

之前:

enter image description here

后:

enter image description here

相似:Limit product short description length in Woocommerce

标签:php,wordpress,woocommerce,product,hook-wordpress
来源: https://codeday.me/bug/20191012/1898151.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有