Woocommerce Short Description Excerpt Length

2019-05-23 06:12发布

I'm trying to change the Short Description excerpt length.

I found a previous post stating that I should change

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

to

<?php $excerpt = apply_filters( 'woocommerce_short_description', $post->post_excerpt );
    echo substr($length,0, 10);
?>

However when doing that, my excerpts simply disappear.

2条回答
三岁会撩人
2楼-- · 2019-05-23 06:32

I'm afraid you're editing the plugin... if that's so, you're doing it wrong..

create a function and then hook to that filter... something like this...

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

paste this on your functions.php file of your theme.

查看更多
爷、活的狠高调
3楼-- · 2019-05-23 06:43

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!)

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.

查看更多
登录 后发表回答