daily pastebin goal
47%
SHARE
TWEET

WooCommerce - Shorten product titles to 5 words

lorro Dec 28th, 2016 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.   // shorten WooCommerce product titles to 5 words
  3.   // code goes in functions.php for your child theme
  4.  
  5.   // not tested by me
  6.  
  7.   add_filter( 'woocommerce_product_title', 'shorten_woocommerce_product_title', 10, 2 );
  8.   function shorten_woocommerce_product_title( $title, $id ) {
  9.     $max_words = 5;
  10.     if ( (is_front_page() || is_shop() || is_product_tag() || is_product_category() )
  11.           && get_post_type( $id ) === 'product' ) {
  12.       $title_words = explode(" ", $title);
  13.       if ( count($title_words) > $max_words ) {
  14.         return implode( " ", array_slice( $title_words, 0, $max_words ) ) . '...';
  15.       } else {
  16.         return $title;
  17.       }
  18.     } else {
  19.       return $title;
  20.     }
  21.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top