Reduce woocommerce excerpt length? - php

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.

Related

Displaying main product image from Woocommerce with a custom plugin

Hi I'm trying to create a custom plugin that will display the main product image from Woocommerce plugin. I want to use shortcode. How do I go about doing this? This is what I had and it didn't work. I've also found different suggestions. I will share everything below
add_shortcode( 'product_image', 'bbloomer_product_image_shortcode' );
function bbloomer_product_reviews_shortcode() {
return woocommerce_get_product_thumbnail();
}
<?php
$gallery_shortcode = '[gallery id="' .intval($post->$post_parent).'"]';
print apply_filters('the_content', $gallery_shortcode);
?>
These are what I found/were suggested to me before
try this:
$featured_image = wp_get_attachment_url( get_post_thumbnail_id($product_id));

Trim Related Products Title Length - Woocommerce

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.

WooCommerce - How to limit the short product description

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.

Woocommerce Short Description Excerpt Length

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.

Preventing WordPress excerpts from hiding videos

I'm using WordPress on my website and I want to limit the posts length on the front page with the_excerpt(); to about 100 words. However if I do another post that has a video will hide the video and I don't want that.
However if I use the_content(); and the <!--more--> tag in the post the Read More link has #more-## after it and causes it to be almost at the bottom of the page.
You can create your own function instead of excerpt
function get_new_excerpt(){
$excerpt = get_the_content();
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$new_excerpt = substr($excerpt, 0, 20);
return $new_excerpt;
}
Place this in functions.php
Now instead of the_excerpt, use
echo get_new_excerpt();

Categories