Limit Number of Words in WP e-Commerce Description and Custom Excerpts

Glenn Glenn's Blog 0 Comments

This was an unanswered question on the WP e-Commerce blog and we needed to create a solution to it for a client project. Oddly WP e-commerce doesn’t limit the number of characters shown in ‘wpsc_the_product_description();’ out of the box and the result is that when you show product descriptions they can look untidy in length and layout. Our solution is to apply a custom filter to trim/limit the words shown and then show a ‘read more’ link for the post/custom post.

Two simple steps:-

1. Add the following code to your functions.php file:-



// Limit Excerpt / Output function string_limit_words($string, $word_limit) {    $words = explode(' ', $string, ($word_limit + 1));    if(count($words) --> $word_limit)
   array_pop($words);
   return implode(' ', $words)."...
(<a href="" .get_permalink($post->ID) ." ">Read more</a>)";
}

2. Locate ‘wpsc_the_product_description(). Often this is part of a php function under div class ‘wpsc_description’ :-



<!--?php //echo do_shortcode(wpsc_the_product_description()); ?-->

Replace with the following code:-


$excerpt = wpsc_the_product_description(); //Text you want to shorten goes here echo string_limit_words($excerpt,20);

In this example the description is limited to 20 words and you can adjust this to your own needs.

Actually you can shorten any output / excerpt in this way by replacing the target (in this case wpsc_the_product_description();”

Custom Excerpt Length

Alternatively for a custom excerpt length you can use:-

1. In Functions.php add



//Custom excerpt length

function excerpt($num) {
 $limit = $num+1;
 $excerpt = explode(' ', get_the_excerpt(), $limit);
 array_pop($excerpt);
 $excerpt = implode(" ",$excerpt)."... (<a href="" .get_permalink($post->ID) ." ">Read more</a>)";
 echo $excerpt;
}

2. Output the Custom Excerpt with:-



php excerpt('20');

This will limit the excerpt to 20 characters.

Change the Excerpt Length in your Template

You can also use the following php snippet to change the excerpt length directly in your template without adding a function to functions.php:-


$excerpt = get_the_excerpt();
$trimmed_excerpt = wp_trim_words( $excerpt, 40, '<a href="'. get_permalink() .'"> ...Read More</a>' );
echo $trimmed_excerpt;

The same snippet can be amended to use the ‘get_the_content’ function to trim the content instead.

Other Useful Excerpt Snippets

Change  the excerpt Read More



// change the excerpt
function new_excerpt_more( $more ) {
	return 'your text or blank';
}
add_filter('excerpt_more', 'new_excerpt_more');

About the Author
Limit Number of Words in WP e-Commerce Description and Custom Excerpts Limit Number of Words in WP e-Commerce Description and Custom Excerpts 0da77002febbfe28d15144a57bbcc2c9 s 180 d mm r g

Glenn

A highly experienced WordPress Web Developer, Front-end and Back-end Developer & New Media Specialist with extensive knowledge of a wide spectrum of technologies in the Development and Creative Industries built up over a number of years.

Leave a Reply

Your email address will not be published. Required fields are marked *