Woocommerce Short Description Excerpt Length - php

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.

Related

Load more without refreshing page [Wordpress]

I am using a WordPress plugin to load single post without refreshing the page, you can find it here: https://wordpress.org/plugins/read-more-without-refresh/
The plugin works perfectly fine when i use it's shortcode, it does exactly what i need it to do but here is the problem, I like to use it on all my post and this means i have to edit over 1000 post. Because i have some rules that i need to apply and there are some contents between the opening and closing shortcode elements , it gets tricky to use "do_shortcode"
Here is the shortcode: [read more="Click here to Read More" less="Read Less"] My Hidden Paragraphs Here [/read]
Here is what i want it to do: I need the plugin to apply the shortcode on all posts, after first 150 words and if the post has less than 150 words, it should do nothing.
Is there anyway i can do this? with php code or the already existing shortcode
You could probably use the_content filter but I haven't tested to see if it renders the shortcode or not.
function my_the_content_filter( $content ) {
if ( is_single() ) {
$word_count = str_word_count( strip_tags( $content ), 2 );
if( count( $word_count ) >= 150 ) {
$word = array_slice( $word_count, 150, 1, true );
$word_pos = key( $word );
$content = substr_replace( $content, '[read more="Click here to Read More" less="Read Less"]', $word_pos, 0 );
$content .= '[/read]';
}
}
return $content;
}
add_filter( 'the_content', 'my_the_content_filter' );

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.

Use wordpress shortcode in post title

I am trying to use the following shortcode in the wordpress post title. The shortcode looks like the following:
//Use [year] in your posts.
function year_shortcode() {
$year = date('Y');
return $year;
}
add_shortcode('year', 'year_shortcode');
Any suggestions how to execute this shortcode in the post title?
I appreciate your replies!
You can absolutely use a shortcode in a title. You just need to use the WordPress hooks system to run the shortcode when the title is called. So if you want to have a shortcode [year] that spits out the current year, you'll create the shortcode:
add_shortcode( 'year', 'sc_year' );
function sc_year(){
return date( 'Y' );
}
Then, hook into the filter for the_title() to run your shortcode:
add_filter( 'the_title', 'my_shortcode_title' );
function my_shortcode_title( $title ){
return do_shortcode( $title );
}
That takes care of the Post/Page title, but you'll also want to run it for the single_post_title hook which is used in wp_head on the title tag on your site. That way, the browser will show the proper title as well:
add_filter( 'single_post_title', 'my_shortcode_title' );
Note: You don't need a separate function here because it's running the exact same code. So your total code would look something like this:
add_shortcode( 'year', 'sc_year' );
function sc_year(){
return date( 'Y' );
}
add_filter( 'single_post_title', 'my_shortcode_title' );
add_filter( 'the_title', 'my_shortcode_title' );
function my_shortcode_title( $title ){
return do_shortcode( $title );
}
I don't think you can apply (save in admin post edit screen) a shortcode in the post title with a shortcode. The post_title is sanitize to avoid tag or shortcode, the post title is used by many function that a shortcode can break.
To make modification on the post_title, you can use the filter the_title
add_filter('the _title', 'yourfunction');
function yourfunction($title){
global $post;
// if you set a custom field on the post where you want to display the year
if(get_post_meta($post->ID, 'display_year', true) == 1){
$title = $title. ' '. date('Y');
}
return $title;
}
Hope it helps
Please add this code in 'function.php'. Try this.
<?php
function TitleFunction($title)
{
global $post;
$title = $title. ' ' .get_the_date('Y');
return $title;
}
add_filter( 'the_title', 'TitleFunction', 10, 2 );
?>

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.

Reduce woocommerce excerpt length?

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.

Categories