Shorten product titles on Woocommerce archive pages
up vote
1
down vote
favorite
I found this code to limit woocommerce product title and its working. But the problem is it applies everywhere even in single product page. How do I make exception not to apply in product page?
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if (get_post_type( $id ) === 'product' ) {
return wp_trim_words( $title, 4, '...' ); // change last number to the number of words you want
} else {
return $title;
}
}
php wordpress woocommerce product title
add a comment |
up vote
1
down vote
favorite
I found this code to limit woocommerce product title and its working. But the problem is it applies everywhere even in single product page. How do I make exception not to apply in product page?
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if (get_post_type( $id ) === 'product' ) {
return wp_trim_words( $title, 4, '...' ); // change last number to the number of words you want
} else {
return $title;
}
}
php wordpress woocommerce product title
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I found this code to limit woocommerce product title and its working. But the problem is it applies everywhere even in single product page. How do I make exception not to apply in product page?
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if (get_post_type( $id ) === 'product' ) {
return wp_trim_words( $title, 4, '...' ); // change last number to the number of words you want
} else {
return $title;
}
}
php wordpress woocommerce product title
I found this code to limit woocommerce product title and its working. But the problem is it applies everywhere even in single product page. How do I make exception not to apply in product page?
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if (get_post_type( $id ) === 'product' ) {
return wp_trim_words( $title, 4, '...' ); // change last number to the number of words you want
} else {
return $title;
}
}
php wordpress woocommerce product title
php wordpress woocommerce product title
edited Nov 10 at 22:15
LoicTheAztec
78.3k125992
78.3k125992
asked Nov 10 at 13:49
Rifat Rahman
113
113
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' ) {
return wp_trim_words( $title, 4, '...' ); // change last number to the number of words you want
} else {
return $title;
}
}
New contributor
Thank you :) Its works
– Rifat Rahman
Nov 11 at 19:00
this code also applied in the backend otherwise its working good. Admin Panel >> Products >> All Products imgur.com/a/GrIMKow
– Rifat Rahman
2 days ago
add a comment |
up vote
0
down vote
The best way is to use woocommerce_shop_loop_item_title
hook:
- First unhooking the product title from loop archive pages.
- Then hooking back a custom version that shorten the title as you want.
This way this shortening will be applied exclusively on product titles on all woocommerce loops (all woocommerce archives pages, related products, upsells, cross sells, shortcodes, widgets…) avoiding changes with single product title.
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action( 'woocommerce_shop_loop_item_title', 'shorten_loop_product_title', 10 );
function shorten_loop_product_title() {
echo '<h2 class="woocommerce-loop-product__title">' . wp_trim_words( get_the_title(), 4, '...' ) . '</h2>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
To shorten product title limiting the number of characters with PHP substr()
function, replace the following line in my code:
echo '<h2 class="woocommerce-loop-product__title">' . wp_trim_words( get_the_title(), 4, '...' ) . '</h2>';
by:
echo '<h2 class="woocommerce-loop-product__title">' . substr( get_the_title(), 0, 25 ) . '...' . '</h2>';
If you want this to be applied only on woocommerce archives pages as shop, you will use:
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action( 'woocommerce_shop_loop_item_title', 'shorten_loop_product_title', 10 );
function shorten_loop_product_title() {
echo '<h2 class="woocommerce-loop-product__title">';
if ( is_shop() || is_product_category() || is_product_tag() ) {
echo wp_trim_words( get_the_title(), 4, '...' );
} else {
echo get_the_title();
}
echo '</h2>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
To shorten product title limiting the number of characters with PHP substr()
function, replace the following line in my code:
echo wp_trim_words( get_the_title(), 4, '...' );
by:
echo substr( get_the_title(), 0, 25 ) . '...';
Addition related to your comment
To shorten product title mimiting the number of characters with PHP substr()
function, replace:
Thanks I'm using this code which is working fine to set the limit except product page. How do I set character limit if I want to your code?// Set title limit without products page add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 ); function shorten_woo_product_title( $title, $id ) { if ( ! is_single( $id ) && get_post_type( $id ) === 'product' ) { return substr( $title, 0, 25 ).'...'; // change last number to the number of words you want } else { return $title; } }
– Rifat Rahman
Nov 11 at 18:58
@RifatRahman I have added an update at the end related to your comment… Have you tried it? If this answer answers your question, you could please accept the answer, Thank you.
– LoicTheAztec
16 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' ) {
return wp_trim_words( $title, 4, '...' ); // change last number to the number of words you want
} else {
return $title;
}
}
New contributor
Thank you :) Its works
– Rifat Rahman
Nov 11 at 19:00
this code also applied in the backend otherwise its working good. Admin Panel >> Products >> All Products imgur.com/a/GrIMKow
– Rifat Rahman
2 days ago
add a comment |
up vote
0
down vote
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' ) {
return wp_trim_words( $title, 4, '...' ); // change last number to the number of words you want
} else {
return $title;
}
}
New contributor
Thank you :) Its works
– Rifat Rahman
Nov 11 at 19:00
this code also applied in the backend otherwise its working good. Admin Panel >> Products >> All Products imgur.com/a/GrIMKow
– Rifat Rahman
2 days ago
add a comment |
up vote
0
down vote
up vote
0
down vote
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' ) {
return wp_trim_words( $title, 4, '...' ); // change last number to the number of words you want
} else {
return $title;
}
}
New contributor
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' ) {
return wp_trim_words( $title, 4, '...' ); // change last number to the number of words you want
} else {
return $title;
}
}
New contributor
edited Nov 10 at 14:43
New contributor
answered Nov 10 at 14:28
Alex Vasilyev
993
993
New contributor
New contributor
Thank you :) Its works
– Rifat Rahman
Nov 11 at 19:00
this code also applied in the backend otherwise its working good. Admin Panel >> Products >> All Products imgur.com/a/GrIMKow
– Rifat Rahman
2 days ago
add a comment |
Thank you :) Its works
– Rifat Rahman
Nov 11 at 19:00
this code also applied in the backend otherwise its working good. Admin Panel >> Products >> All Products imgur.com/a/GrIMKow
– Rifat Rahman
2 days ago
Thank you :) Its works
– Rifat Rahman
Nov 11 at 19:00
Thank you :) Its works
– Rifat Rahman
Nov 11 at 19:00
this code also applied in the backend otherwise its working good. Admin Panel >> Products >> All Products imgur.com/a/GrIMKow
– Rifat Rahman
2 days ago
this code also applied in the backend otherwise its working good. Admin Panel >> Products >> All Products imgur.com/a/GrIMKow
– Rifat Rahman
2 days ago
add a comment |
up vote
0
down vote
The best way is to use woocommerce_shop_loop_item_title
hook:
- First unhooking the product title from loop archive pages.
- Then hooking back a custom version that shorten the title as you want.
This way this shortening will be applied exclusively on product titles on all woocommerce loops (all woocommerce archives pages, related products, upsells, cross sells, shortcodes, widgets…) avoiding changes with single product title.
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action( 'woocommerce_shop_loop_item_title', 'shorten_loop_product_title', 10 );
function shorten_loop_product_title() {
echo '<h2 class="woocommerce-loop-product__title">' . wp_trim_words( get_the_title(), 4, '...' ) . '</h2>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
To shorten product title limiting the number of characters with PHP substr()
function, replace the following line in my code:
echo '<h2 class="woocommerce-loop-product__title">' . wp_trim_words( get_the_title(), 4, '...' ) . '</h2>';
by:
echo '<h2 class="woocommerce-loop-product__title">' . substr( get_the_title(), 0, 25 ) . '...' . '</h2>';
If you want this to be applied only on woocommerce archives pages as shop, you will use:
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action( 'woocommerce_shop_loop_item_title', 'shorten_loop_product_title', 10 );
function shorten_loop_product_title() {
echo '<h2 class="woocommerce-loop-product__title">';
if ( is_shop() || is_product_category() || is_product_tag() ) {
echo wp_trim_words( get_the_title(), 4, '...' );
} else {
echo get_the_title();
}
echo '</h2>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
To shorten product title limiting the number of characters with PHP substr()
function, replace the following line in my code:
echo wp_trim_words( get_the_title(), 4, '...' );
by:
echo substr( get_the_title(), 0, 25 ) . '...';
Addition related to your comment
To shorten product title mimiting the number of characters with PHP substr()
function, replace:
Thanks I'm using this code which is working fine to set the limit except product page. How do I set character limit if I want to your code?// Set title limit without products page add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 ); function shorten_woo_product_title( $title, $id ) { if ( ! is_single( $id ) && get_post_type( $id ) === 'product' ) { return substr( $title, 0, 25 ).'...'; // change last number to the number of words you want } else { return $title; } }
– Rifat Rahman
Nov 11 at 18:58
@RifatRahman I have added an update at the end related to your comment… Have you tried it? If this answer answers your question, you could please accept the answer, Thank you.
– LoicTheAztec
16 hours ago
add a comment |
up vote
0
down vote
The best way is to use woocommerce_shop_loop_item_title
hook:
- First unhooking the product title from loop archive pages.
- Then hooking back a custom version that shorten the title as you want.
This way this shortening will be applied exclusively on product titles on all woocommerce loops (all woocommerce archives pages, related products, upsells, cross sells, shortcodes, widgets…) avoiding changes with single product title.
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action( 'woocommerce_shop_loop_item_title', 'shorten_loop_product_title', 10 );
function shorten_loop_product_title() {
echo '<h2 class="woocommerce-loop-product__title">' . wp_trim_words( get_the_title(), 4, '...' ) . '</h2>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
To shorten product title limiting the number of characters with PHP substr()
function, replace the following line in my code:
echo '<h2 class="woocommerce-loop-product__title">' . wp_trim_words( get_the_title(), 4, '...' ) . '</h2>';
by:
echo '<h2 class="woocommerce-loop-product__title">' . substr( get_the_title(), 0, 25 ) . '...' . '</h2>';
If you want this to be applied only on woocommerce archives pages as shop, you will use:
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action( 'woocommerce_shop_loop_item_title', 'shorten_loop_product_title', 10 );
function shorten_loop_product_title() {
echo '<h2 class="woocommerce-loop-product__title">';
if ( is_shop() || is_product_category() || is_product_tag() ) {
echo wp_trim_words( get_the_title(), 4, '...' );
} else {
echo get_the_title();
}
echo '</h2>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
To shorten product title limiting the number of characters with PHP substr()
function, replace the following line in my code:
echo wp_trim_words( get_the_title(), 4, '...' );
by:
echo substr( get_the_title(), 0, 25 ) . '...';
Addition related to your comment
To shorten product title mimiting the number of characters with PHP substr()
function, replace:
Thanks I'm using this code which is working fine to set the limit except product page. How do I set character limit if I want to your code?// Set title limit without products page add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 ); function shorten_woo_product_title( $title, $id ) { if ( ! is_single( $id ) && get_post_type( $id ) === 'product' ) { return substr( $title, 0, 25 ).'...'; // change last number to the number of words you want } else { return $title; } }
– Rifat Rahman
Nov 11 at 18:58
@RifatRahman I have added an update at the end related to your comment… Have you tried it? If this answer answers your question, you could please accept the answer, Thank you.
– LoicTheAztec
16 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
The best way is to use woocommerce_shop_loop_item_title
hook:
- First unhooking the product title from loop archive pages.
- Then hooking back a custom version that shorten the title as you want.
This way this shortening will be applied exclusively on product titles on all woocommerce loops (all woocommerce archives pages, related products, upsells, cross sells, shortcodes, widgets…) avoiding changes with single product title.
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action( 'woocommerce_shop_loop_item_title', 'shorten_loop_product_title', 10 );
function shorten_loop_product_title() {
echo '<h2 class="woocommerce-loop-product__title">' . wp_trim_words( get_the_title(), 4, '...' ) . '</h2>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
To shorten product title limiting the number of characters with PHP substr()
function, replace the following line in my code:
echo '<h2 class="woocommerce-loop-product__title">' . wp_trim_words( get_the_title(), 4, '...' ) . '</h2>';
by:
echo '<h2 class="woocommerce-loop-product__title">' . substr( get_the_title(), 0, 25 ) . '...' . '</h2>';
If you want this to be applied only on woocommerce archives pages as shop, you will use:
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action( 'woocommerce_shop_loop_item_title', 'shorten_loop_product_title', 10 );
function shorten_loop_product_title() {
echo '<h2 class="woocommerce-loop-product__title">';
if ( is_shop() || is_product_category() || is_product_tag() ) {
echo wp_trim_words( get_the_title(), 4, '...' );
} else {
echo get_the_title();
}
echo '</h2>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
To shorten product title limiting the number of characters with PHP substr()
function, replace the following line in my code:
echo wp_trim_words( get_the_title(), 4, '...' );
by:
echo substr( get_the_title(), 0, 25 ) . '...';
Addition related to your comment
To shorten product title mimiting the number of characters with PHP substr()
function, replace:
The best way is to use woocommerce_shop_loop_item_title
hook:
- First unhooking the product title from loop archive pages.
- Then hooking back a custom version that shorten the title as you want.
This way this shortening will be applied exclusively on product titles on all woocommerce loops (all woocommerce archives pages, related products, upsells, cross sells, shortcodes, widgets…) avoiding changes with single product title.
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action( 'woocommerce_shop_loop_item_title', 'shorten_loop_product_title', 10 );
function shorten_loop_product_title() {
echo '<h2 class="woocommerce-loop-product__title">' . wp_trim_words( get_the_title(), 4, '...' ) . '</h2>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
To shorten product title limiting the number of characters with PHP substr()
function, replace the following line in my code:
echo '<h2 class="woocommerce-loop-product__title">' . wp_trim_words( get_the_title(), 4, '...' ) . '</h2>';
by:
echo '<h2 class="woocommerce-loop-product__title">' . substr( get_the_title(), 0, 25 ) . '...' . '</h2>';
If you want this to be applied only on woocommerce archives pages as shop, you will use:
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action( 'woocommerce_shop_loop_item_title', 'shorten_loop_product_title', 10 );
function shorten_loop_product_title() {
echo '<h2 class="woocommerce-loop-product__title">';
if ( is_shop() || is_product_category() || is_product_tag() ) {
echo wp_trim_words( get_the_title(), 4, '...' );
} else {
echo get_the_title();
}
echo '</h2>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
To shorten product title limiting the number of characters with PHP substr()
function, replace the following line in my code:
echo wp_trim_words( get_the_title(), 4, '...' );
by:
echo substr( get_the_title(), 0, 25 ) . '...';
Addition related to your comment
To shorten product title mimiting the number of characters with PHP substr()
function, replace:
edited Nov 11 at 21:01
answered Nov 10 at 22:13
LoicTheAztec
78.3k125992
78.3k125992
Thanks I'm using this code which is working fine to set the limit except product page. How do I set character limit if I want to your code?// Set title limit without products page add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 ); function shorten_woo_product_title( $title, $id ) { if ( ! is_single( $id ) && get_post_type( $id ) === 'product' ) { return substr( $title, 0, 25 ).'...'; // change last number to the number of words you want } else { return $title; } }
– Rifat Rahman
Nov 11 at 18:58
@RifatRahman I have added an update at the end related to your comment… Have you tried it? If this answer answers your question, you could please accept the answer, Thank you.
– LoicTheAztec
16 hours ago
add a comment |
Thanks I'm using this code which is working fine to set the limit except product page. How do I set character limit if I want to your code?// Set title limit without products page add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 ); function shorten_woo_product_title( $title, $id ) { if ( ! is_single( $id ) && get_post_type( $id ) === 'product' ) { return substr( $title, 0, 25 ).'...'; // change last number to the number of words you want } else { return $title; } }
– Rifat Rahman
Nov 11 at 18:58
@RifatRahman I have added an update at the end related to your comment… Have you tried it? If this answer answers your question, you could please accept the answer, Thank you.
– LoicTheAztec
16 hours ago
Thanks I'm using this code which is working fine to set the limit except product page. How do I set character limit if I want to your code?
// Set title limit without products page add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 ); function shorten_woo_product_title( $title, $id ) { if ( ! is_single( $id ) && get_post_type( $id ) === 'product' ) { return substr( $title, 0, 25 ).'...'; // change last number to the number of words you want } else { return $title; } }
– Rifat Rahman
Nov 11 at 18:58
Thanks I'm using this code which is working fine to set the limit except product page. How do I set character limit if I want to your code?
// Set title limit without products page add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 ); function shorten_woo_product_title( $title, $id ) { if ( ! is_single( $id ) && get_post_type( $id ) === 'product' ) { return substr( $title, 0, 25 ).'...'; // change last number to the number of words you want } else { return $title; } }
– Rifat Rahman
Nov 11 at 18:58
@RifatRahman I have added an update at the end related to your comment… Have you tried it? If this answer answers your question, you could please accept the answer, Thank you.
– LoicTheAztec
16 hours ago
@RifatRahman I have added an update at the end related to your comment… Have you tried it? If this answer answers your question, you could please accept the answer, Thank you.
– LoicTheAztec
16 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239620%2fshorten-product-titles-on-woocommerce-archive-pages%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password