0

How Can I limit the visible part of the 'short description' in the Category Page?

I found the code in theme/woocommerce/content-product.php, but whatever I try doesn't do a thing.

<?php
        $len = strlen(strip_tags($post->post_excerpt));
        if($len>get_option('shop_dec_len', '180')) {
            $len = "...";
        } else {
            $len = "";
        }
    ?>
    <?php if ($post->post_excerpt && $post->post_excerpt != "") echo '<div     itemprop="description" class="desc">' . mb_substr ( strip_tags($post->post_excerpt), 0, get_option('shop_dec_len', '180')) . $len . '</div>'; ?>
1

Normally if you set a custom excerpt it shows the full text of that excerpt. In this case the length is being limited by the PHP function mb_substr to a limit of 180 characters or whatever the shop_dec_len option is set to. This tells me that you can either modify that parameter of mb_substr directly or there is an option somewhere in the admin and you don't even need to change the code. To modify it directly you could do the following:

<?php if ($post->post_excerpt && $post->post_excerpt != "") echo '<div     itemprop="description" class="desc">' . mb_substr ( strip_tags($post->post_excerpt), 0, 100 ) . $len . '</div>'; ?>

For a different way to trim the excerpt, you might look into [trim_word()][2] which is a neat function someone wrote for trimming without breaking words:

/**
 * trims text to a space then adds ellipses if desired
 * @param string $input text to trim
 * @param int $length in characters to trim to
 * @param bool $ellipses if ellipses (...) are to be added
 * @param bool $strip_html if html tags are to be stripped
 * @return string 
 */
function trim_text($input, $length, $ellipses = true, $strip_html = true) {
    //strip tags, if desired
    if ($strip_html) {
        $input = strip_tags($input);
    }

    //no need to trim, already shorter than trim length
    if (strlen($input) <= $length) {
        return $input;
    }

    //find last space within length
    $last_space = strrpos(substr($input, 0, $length), ' ');
    $trimmed_text = substr($input, 0, $last_space);

    //add ellipses (...)
    if ($ellipses) {
        $trimmed_text .= '...';
    }

    return $trimmed_text;
}

So that might look like:

<?php if ($post->post_excerpt && $post->post_excerpt != "") echo '<div     itemprop="description" class="desc">' . trim_words( $post->post_excerpt, 100, false, true ) . $len . '</div>'; ?>
  • Ok thanks alot. I'll try to implement your solution or at least find out where I can set the excerpt length. – OAK Oct 29 '13 at 18:00
  • 1
    Ok. If this answers your question, don't forget to come back and accept the answer. – helgatheviking Oct 29 '13 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.