Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce Short Description Excerpt Length

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.

like image 555
Robert Miller Avatar asked Dec 19 '22 19:12

Robert Miller


2 Answers

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.

like image 132
Reigel Avatar answered Jan 01 '23 10:01

Reigel


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.

like image 38
Swapnali Avatar answered Jan 01 '23 12:01

Swapnali