Woocommerce Short Description Excerpt Length

12,795

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.

Share:
12,795

Related videos on Youtube

Robert Miller
Author by

Robert Miller

Language lover, I'm working on a project to teach French!

Updated on August 05, 2022

Comments

  • Robert Miller
    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
      Igor Yavych about 8 years
      look 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
    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
    Robert Miller about 8 years
    That'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
    Reigel Gallarde about 8 years
    if you remove that line of code ,<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ); ?>, will the excerpt display still?
  • Reigel Gallarde
    Reigel Gallarde about 8 years
    Ahh I made a typo on my code.. try and copy my updated code... again without editing product.php
  • Robert Miller
    Robert Miller about 8 years
    Excellent! That worked! Thank you! Would happen to know how I could limit that function to archive pages and not the product page?
  • Dmitry Yudin
    Dmitry Yudin over 6 years
    Saved my day! THANK YOU
  • Elijah
    Elijah over 6 years
    Is 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.

Related