in WooCommerce, How can I limit the short product description in the shop page? i added this code:
add_action('woocommerce_after_shop_loop_item_title','woocommerce_template_single_excerpt', 5);
but can't limit it to 40 charachters.
Thanks!!!
Instead of editing files directly within the plugin (which is a very bad idea because once update the plugin and all of your changes will be lost!)
add_filter('woocommerce_short_description', 'limit_woocommerce_short_description', 10, 1);
function limit_woocommerce_short_description($post_excerpt){
if (!is_product()) {
$post_excerpt = substr($post_excerpt, 0, 20);
}
return $post_excerpt;
}
Then paste this in your functions.php file of your theme.
and use this line where you want to display product description -
<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ); ?>
use this code to change Limit on the Shop Page not Product-detailed Page.
I would use:
function et_excerpt_length($length) {
global $post;
if($post->post_type=="product") return 40;
return 20; /*Your default excerpt length*/
}
add_filter('excerpt_length', 'et_excerpt_length');
adding it in function.php
You can use this code for limit no of words -
add_filter('woocommerce_short_description', 'limit_woocommerce_short_description', 10, 1);
function limit_woocommerce_short_description($post_excerpt){
if (!is_product()) {
$pieces = explode(" ", $post_excerpt);
$post_excerpt = implode(" ", array_splice($pieces, 0, 20));
}
return $post_excerpt;
}
explode breaks the original string into an array of words, array_splice lets you get certain ranges of those words, and then implode combines the ranges back together into single strings.
use this code to change Limit on the Shop Page not Product-detailed Page.
Related
I have a site where I need to change the product title to remove the word'`-product' from the product title when it is shown.
For example the product name is stored as "1000 Piece Puzzle -Product" when the shopper comes to the front page we want them to see "1000 Piece Puzzle" (without the "-product")
I tried
remove_action('woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title',10);
add_action('woocommerce_shop_loop_item_title','fun',10);
function fun($ttl)
{
echo str_replace('-product', '', $ttl);;
}
However it would not work. Am I going about this the wrong way?
You can accomplish that by adding a filter to change the product title. Instead of using an action.
add_filter( 'the_title', 'change_product_title', 10, 2 );
function change_product_title( $title, $id ) {
$Find = '-product';
$ReplaceWith = '';
//REMOVE DESIRED STRING FROM PRODUCT TITLE
$title = str_replace($Find, $ReplaceWith, $title);
return $title;
}
I currently have an issue with trimming the length of the product title in the "Related Products" section of my single product page.
I have set up a custom hook pointing to a custom template for this. The only problem is that it isn't trimming any of it - it is still printing the whole title.
I have tried to use get_the_title() and it doesn't print anything to screen, so in the code below I have tried to pass the title as a string and not an array.
What am I doing wrong?
<?php $title = the_title('<h3 class="product_title entry-title">', '</h3>');
$text = wp_trim_words($title, 2, '...')
?>
<?php echo $text; ?>
Thanks in advance
Regards
Michael
the_title() will be auto print as you don't need to echo it.
You need to use filter for title as the following in functions.php,
function trim_title( $title ) {
$title = wp_trim_words( $title , 40, '...' );
return $title;
}
add_filter( 'the_title', 'trim_title', 10, 1 );
This solution doesn't work in up-sells.php even if it's almost the same as related.php , instead of this whole function trim_title( $title ) in both files after $post_object = get_post( $upsell->get_id() ); (or $related_product) I added $post_object->post_title = substr( $post_object->post_title, 0, 50) . '…'; and it works. It limits to 50 characters, you can change.
I need to set the limit of the short description on the related product list.
I have done this for the product list page by using the filter:
function my_filter_woocommerce_short_description( $post_excerpt ) {
if(is_product()){
}else{
$post_excerpt = get_the_content();
$post_excerpt = preg_replace(" (\[.*?\])",'',$post_excerpt);
$post_excerpt = strip_shortcodes($post_excerpt);
$post_excerpt = strip_tags($post_excerpt);
$post_excerpt = substr($post_excerpt, 0, 265);
$post_excerpt = substr($post_excerpt, 0, strripos($post_excerpt, " "));
$post_excerpt = trim(preg_replace( '/\s+/', ' ', $post_excerpt));
$post_excerpt = $post_excerpt.'... more';
//$post_excerpt=substr($post_excerpt , 0 ,265);
return $post_excerpt.'...';
}
}
add_filter( 'woocommerce_short_description','my_filter_woocommerce_short_description',10, 2 );
however I need to display the full short description in the product page, so inside this filter I created a if is product.
The problem is inside product page I also have a list of related products which I need to display in a short way as it is being displayed in the product list page.
How can i do it?
Thank you
Instead of the filter you used, try the action 'woocommerce_after_shop_loop_item_title' this way
add_action('woocommerce_after_shop_loop_item_title','short_description_content', 5);
function short_description_content(){
//code for short description content
}
This way, you don't need to check if it is single product or product list.
To show short description in the related product list as you said, just add 'the_excerpt' in the above function
function short_description_content(){
the_excerpt();
}
I'm trying to change the Short Description excerpt length.
I found a previous post stating that I should change
<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?>
to
<?php $excerpt = apply_filters( 'woocommerce_short_description', $post->post_excerpt );
echo substr($length,0, 10);
?>
However when doing that, my excerpts simply disappear.
I'm afraid you're editing the plugin... if that's so, you're doing it wrong..
create a function and then hook to that filter... something like this...
add_filter('woocommerce_short_description', 'reigel_woocommerce_short_description', 10, 1);
function reigel_woocommerce_short_description($post_excerpt){
if (!is_product()) {
$post_excerpt = substr($post_excerpt, 0, 10);
}
return $post_excerpt;
}
paste this on your functions.php file of your theme.
Instead of editing files directly within the plugin (which is a very bad idea because once update the plugin and all of your changes will be lost!)
You can use this code for limit no of words -
add_filter('woocommerce_short_description', 'limit_woocommerce_short_description', 10, 1);
function limit_woocommerce_short_description($post_excerpt){
if (!is_product()) {
$pieces = explode(" ", $post_excerpt);
$post_excerpt = implode(" ", array_splice($pieces, 0, 20));
}
return $post_excerpt;
}
explode breaks the original string into an array of words, array_splice lets you get certain ranges of those words, and then implode combines the ranges back together into single strings.
use this code to change Limit on the Shop Page not Product-detailed Page.
So, i am currently working with the theme Neighborhood and it seems like when i choose to show description under the products it shows the whole thing.
Is there any way to limit the Excerpt length for that? I've tried some different codes that i've put into funtions.php and also tried to edit the short-description.php in the woocommerce folder to no avail.
i've tried this code
<?php $excerpt = apply_filters( 'woocommerce_short_description', $post->post_excerpt );
echo substr($length,0, 10);
?>
Is it something that i am missing here i wonder?
Looks like you need a custom function
function get_the_twitter_excerpt(){
$excerpt = get_the_content();
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$the_str = substr($excerpt, 0, 20);
return $the_str;
}
you should be able to put the above into your functions and this
<?php echo 'Excerpt: '.get_the_twitter_excerpt(); ?>
into your loop.