Trim Related Products Title Length - Woocommerce - php

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.

Related

WooCommerce change product title?

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;
}

When changing woocommerce title hook the first item doesn't change

I have a strange behaviour that I don't understand
I've changed the woocommerce_shop_loop_item_title hook to add a link to the title of the product. This is my code inside functions.php
// Add HREF TO TITLE
function abChangeProductsTitleHook(){
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action('woocommerce_shop_loop_item_title', 'abChangeProductsTitle', 10 );
}
add_action( 'woocommerce_shop_loop_item_title', 'abChangeProductsTitleHook' );
function abChangeProductsTitle() {
echo '<h2 class="woocommerce-loop-product_title">' . get_the_title() . '</h2>';
}
It works perfectly on all the products except the first one.
I've also made a similar change to another hook to change the thumbnail image to a background image and also this one doesn't work on the first product. It's always the first product even if I change the order of the products.
Below you see a screenshot of the the first row of products on the page and that the first one is displayed differently
It would be really helpful if anyone knows that problem or can point me in the right direction .
Thank you very much
Alex
The way you are removing and adding the woocommerce_shop_loop_item_title is the problem. Try it this way.
remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
add_action('woocommerce_shop_loop_item_title', 'abChangeProductsTitle', 10 );
function abChangeProductsTitle() {
echo '<h2 class="woocommerce-loop-product_title">' . get_the_title() . '</h2>';
}
Although the accepted answer works, but why do we have to remove the title and then add it back in again? We could simply filter it in the first place.
add_filter('the_title', 'your_shop_custom_title', 20, 2);
function your_shop_custom_title($the_title, $id)
{
if (is_shop() && get_post_type($id) == 'product') : // runs only on the shop page
$the_title = '' . $the_title . '';
endif;
return $the_title;
}

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 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.

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