WooCommerce Product Description Length Limit

I use the following code on my WordPress site to shorten my description of excerpts from WooCommerce, and it works great if I enter my characters for 14 or less. As soon as I enter more than 14 characters, it shows a complete short description.

add_action( 'woocommerce_after_shop_loop_item_title', 'lk_woocommerce_product_excerpt', 35, 2);
if (!function_exists('lk_woocommerce_product_excerpt'))
{
    function lk_woocommerce_product_excerpt()
    {
        $content_length = 14;
        global $post;
        $content = $post->post_excerpt;
        $wordarray = explode(' ', $content, $content_length + 1);
        if(count($wordarray) > $content_length) :
            array_pop($wordarray);
            array_push($wordarray, '...');
            $content = implode(' ', $wordarray);
            $content = force_balance_tags($content);
            $content = substr($content, 0, 14);

        endif;
        echo "<span class='excerpt'><p>$content...</p></span>";
    }
}

Any help would be greatly appreciated.

Thank.

+4
source share
2 answers

- - (Updated) - -

, , , . , php ( , 25 ). , :

add_action( 'woocommerce_after_shop_loop_item_title', 'lk_woocommerce_product_excerpt', 35, 2);
if (!function_exists('lk_woocommerce_product_excerpt'))
{
    function lk_woocommerce_product_excerpt()
    {
        global $post;
        $limit = 14;
        $text = $post->post_excerpt;
        if (str_word_count($text, 0) > $limit) {
            $arr = str_word_count($text, 2);
            $pos = array_keys($arr);
            $text = substr($text, 0, $pos[$limit]) . '...';
            // $text = force_balance_tags($text); // may be you dont need this…
        }
        echo '<span class="excerpt"><p>' . $text . '</p></span>';
    }
}

, :

if (!function_exists('lk_limit_text'))
{
    function lk_limit_text($text, $limit) {
        if (str_word_count($text, 0) > $limit) {
            $words = str_word_count($text, 2);
            $pos = array_keys($words);
            $text = substr($text, 0, $pos[$limit]) . '...';
        }
        return $text;
    }
}

add_action( 'woocommerce_after_shop_loop_item_title', 'lk_woocommerce_product_excerpt', 35, 2);
if (!function_exists('lk_woocommerce_product_excerpt'))
{
    function lk_woocommerce_product_excerpt()
    {
        global $post;
        $content = $post->post_excerpt;
        // $content = force_balance_tags($content); // may be you dont need this…
        echo '<span class="excerpt"><p>' . lk_limit_text( $content, 14 ) . '</p></span>';
    }
}

...

: 20 PHP?

+2

WooCommerce

-

add_filter('woocommerce_short_description', 'limit_product_short_description', 10, 1);

function limit_product_short_description($post_excerpt)
 {
    if (!is_product()) 
    {
         $pieces = explode(" ", $post_excerpt);
         $post_excerpt = implode(" ", array_splice($pieces, 0, 14));
     }
   return $post_excerpt;
 }

explode , array_splice , implode .

, .

, if (!is_product()).

0

All Articles