Shorten product title (product title) in woocommerce – Learn WordPress from a to z

Tutorials 0 lượt xem
Shorten product title (product title) in woocommerce - Learn Wordpress from a to z thumbnail

How to shorten product title in woocommerce?

Sometimes your product title is quite long

product title

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,); // thay đổi số từ bạn muốn thêm
} else {
return $title;
}
}

the result will be like this:

:how to shorten product title in woocommerce.

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!

Bài viết liên quan