How to shorten product title in woocommerce?
Sometimes your product title is quite long

and now you just want to shorten the title products displayed in woocommerce.
Today I will share with you a function to solve this problem:
This is the code, please copy it to the functions.php file:
add_filter( 'the_title', 'short_title_product', 10, 2 );
function short_title_product( $title, $id ) {
if (get_post_type( $id ) === 'product' & !is_single() ) {
return wp_trim_words( $title, 2 ); // thay đổi số từ bạn muốn thêm
} else {
return $title;
}
}
the result will be like this:

As you can see the title has only 2 words.
Code explanation:
- add_filter: filter hook needs to be changed if you don’t know, you can read what action and filter are in wordpress.
- the_title: filter hook name.
-
short_title_product: function declaration.
Epilogue
Hopefully the tutorial on how to shorten the product title (product title) in woocommerce.
If you find it interesting, you can follow the wordpress tips section to know more new knowledge.
Follow fanpage to receive the latest posts: Group
Wish you have interesting and interesting knowledge about wordpress!
