Advertisement
lorro

WooCommerce - Shorten product titles to 5 words

Dec 28th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.71 KB | None | 0 0
  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.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement