Advertisement
fahimmurshed

How to limit WooCommerce product title

May 18th, 2020 (edited)
1,275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.77 KB | None | 0 0
  1. // Limit WooCommerce Title
  2. // More info on https://fahimm.com
  3. add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
  4. function shorten_woo_product_title( $title, $id ) {
  5.     if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' ) {
  6.         return wp_trim_words( $title, 4, '...' ); // change last number to the number of words you want
  7.     } else {
  8.         return $title;
  9.     }
  10. }
  11.  
  12. // You can try this.
  13. // Limit WooCommerce product titles
  14. function limit_woocommerce_product_title($title, $id = null) {
  15.     $limit = 40;  // Set the character limit you prefer
  16.     if (strlen($title) > $limit) {
  17.         $title = substr($title, 0, $limit) . '...';
  18.     }
  19.     return $title;
  20. }
  21. add_filter('the_title', 'limit_woocommerce_product_title', 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement