简体   繁体   English

WooCommerce - 如何限制简短的产品描述

[英]WooCommerce - How to limit the short product description

in WooCommerce, How can I limit the short product description in the shop page?在 WooCommerce 中,如何限制商店页面中的简短产品描述? i added this code:我添加了这个代码:

add_action('woocommerce_after_shop_loop_item_title','woocommerce_template_single_excerpt', 5);

but can't limit it to 40 charachters.但不能将其限制为 40 个字符。

Thanks!!!谢谢!!!

Instead of editing files directly within the plugin (which is a very bad idea because once update the plugin and all of your changes will be lost!)而不是直接在插件中编辑文件(这是一个非常糟糕的主意,因为一旦更新插件,您的所有更改都将丢失!)

add_filter('woocommerce_short_description', 'limit_woocommerce_short_description', 10, 1);
function limit_woocommerce_short_description($post_excerpt){
    if (!is_product()) {
        $post_excerpt = substr($post_excerpt, 0, 20);
    }
    return $post_excerpt;
}

Then paste this in your functions.php file of your theme.然后将其粘贴到主题的functions.php文件中。

and use this line where you want to display product description -并在要显示产品说明的地方使用此行 -

<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ); ?> 

use this code to change Limit on the Shop Page not Product-detailed Page .使用此代码更改 Shop Page 而不是 Product-detailed Page 的限制

I would use:我会用:

function et_excerpt_length($length) {
    global $post;
    if($post->post_type=="product") return 40;
        return 20; /*Your default excerpt length*/
}
add_filter('excerpt_length', 'et_excerpt_length');

adding it in function.php在 function.php 中添加它

You can use this code for limit no of words -您可以使用此代码限制字数 -

add_filter('woocommerce_short_description', 'limit_woocommerce_short_description', 10, 1);
function limit_woocommerce_short_description($post_excerpt){
    if (!is_product()) {
        $pieces = explode(" ", $post_excerpt);
        $post_excerpt = implode(" ", array_splice($pieces, 0, 20));

    }
    return $post_excerpt;
}

explode breaks the original string into an array of words, array_splice lets you get certain ranges of those words, and then implode combines the ranges back together into single strings. explode将原始字符串分解为一个单词数组,array_splice 可以让您获得这些单词的某些范围,然后implode将这些范围组合成单个字符串。

use this code to change Limit on the Shop Page not Product-detailed Page.使用此代码更改商店页面上的限制,而不是产品详细页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM