Adding a short description in the WooCommerce product catalog

Creating a function linked to the hook:

add_action( 'woocommerce_after_shop_loop_item_title', 'add_short_description', 9 );
function add_short_description() {
	  echo  the_excerpt().'<br />';
}

If you want the short description to be cropped, the code will look like this:


add_action( 'woocommerce_after_shop_loop_item_title', 'add_short_description', 9 );
function add_short_description() {
    global $post;
    $text = $post->post_excerpt;
    $maxchar = 100; //maximum number of characters
    
    $text = preg_replace ('~\[[^\]]+\]~', '', $text ); //removing shortcodes
    
    //deleting all html characters
    $text = strip_tags( $text);
    
    // Cut
    if ( mb_strlen( $text ) > $maxchar ){
                $text = mb_substr( $text, 0, $maxchar );
                $text = preg_replace('@(.*)\s[^\s]*$@s', '\\1 ...', $text );
            }
    echo $text;
}

Updated: 29.04.2020

Did this article help you? Rate it!
(No Ratings Yet)
Loading...

Support on Patreon https://www.patreon.com/processby

Leave a Reply

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

Contacts

E-mail: processby@gmail.com
Skype: inprocess.by
Belarus, Grodno

Send message