简体   繁体   中英

WooCommerce - How to limit the short product description

in WooCommerce, How can I limit the short product description in the shop page? 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.

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.

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 .

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

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.

use this code to change Limit on the Shop Page not Product-detailed Page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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