Woocommerce Short Description Excerpt Length
Solution 1
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.
Solution 2
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.
Related videos on Youtube
Robert Miller
Language lover, I'm working on a project to teach French!
Updated on August 05, 2022Comments
-
Robert Miller over 1 year
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.
-
Igor Yavych about 8 yearslook carefully at the name of your variable where you store your excerpt and at the first param of
substr
. See some difference? :P
-
-
Robert Miller about 8 years<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ); ?> is in "content-product.php", which is located in my theme. I'm afraid that this function created the same issue and my excerpts disappeared.
-
Robert Miller about 8 yearsThat's right, the content hasn't been changed, yet this function isn't working and is left with the original <?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ); ?>.
-
Reigel Gallarde about 8 yearsif you remove that line of code ,
<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ); ?>
, will the excerpt display still? -
Reigel Gallarde about 8 yearsAhh I made a typo on my code.. try and copy my updated code... again without editing
product.php
-
Robert Miller about 8 yearsExcellent! That worked! Thank you! Would happen to know how I could limit that function to archive pages and not the product page?
-
Dmitry Yudin over 6 yearsSaved my day! THANK YOU
-
Elijah over 6 yearsIs there any way to place ... (three comas) in the end of the excerpt to indicate that it is not fully quoted but rather cut off? Thank you.