Reputation: 367
I want to replace the excerpt of a product with the long description. Right now i'm using the following code:
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_single_product_summary', 'the_content', 10 );
The above code does the job, however, it displays the full description. I would like to somehow limit the words (length) that is displayed and add a "read more" button at the end.
Upvotes: 2
Views: 2309
Reputation: 850
Just create a new function to process the value of get_the_content() to get only a max number of words and add a "Read more" link at the end:
function custom_single_product_summary(){
$maxWords = 50; // Change this to your preferences
$description = strip_tags(get_the_content()); // Remove HTML to get the plain text
$words = explode(' ', $description);
$trimmedWords = array_slice($words, 0, $maxWords);
$trimmedText = join(' ', $trimmedWords);
if(strlen($trimmedText) < strlen($description)){
$trimmedText .= ' — <a href="' . get_permalink() . '">Read More</a>';
}
echo $trimmedText;
}
Then use it in the original override code you were trying to use:
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_single_product_summary', 'custom_single_product_summary', 10 );
UPDATED ANSWER: Changed the action hook to echo the value rather than return it, as WooCommerce expects the action to print the output.
Upvotes: 2