Reputation: 151
I am trying to limit the number of words of the product category description in woocommerce on the product category page and add a read more link to expand the text. I have been trying to edit a product description character limit with no luck. Any help would be greatly appreciated. Here is the code i have been editing in the functions.php:
add_action('woocommerce_product_archive_description', 'description_in_shop_loop_item', 3 );
function description_in_shop_loop_item() {
global $shop_page;
// HERE define the number of characters
$limit = 75;
$description = $shop_page->post_content; // category description
// Limit the characters length
if (strlen($description) > $limit) {
$excerpt = substr($description, 0, $limit) . '...';
} else {
$excerpt = $description;
}
echo '<p class="description">'.$excerpt.'</p>';
}
Upvotes: 4
Views: 1524
Reputation: 11
This is how I implemented on my website. Thanks Terminator-barbapapa
if($(window).width() <= 480){
//Get current height of the description container
let curHeight = $(".my-expender").height();
//Set heigth of the description container
$(".my-expender").css({
"height": "900px",
"overflow": "hidden",
});
//Add 'Read more' button
$(".my-expender").after( "<div class='myrd-wrapper'><div class='my-readmore-gradient'></div><button class='read-more'>READ FULL ARTICLE</button></div>" );
//Set description container back to its original height
$( ".read-more" ).on( "click", function() {
$(".my-expender").animate({height: curHeight});
// hide button after the button click
$('.read-more').hide();
$('.my-readmore-gradient').hide();
});
}
Upvotes: 0
Reputation: 3126
You could use some jQuery to shrink the height of the description container and add a 'Read more' button that will resize the container back to its original height.
add_action( 'woocommerce_after_main_content', 'woocommerce_after_main_content', 10 );
function woocommerce_after_main_content() {
if ( !is_product_category() ) return;
?>
<script>
jQuery( function( $ ) {
$(document).ready(function() {
//Get current height of the description container
let curHeight = $("header .term-description").height();
//Set heigth of the description container
$("header .term-description").css({
"height": "100px",
"overflow": "hidden",
});
//Add 'Read more' button
$("header .term-description").after( "<button class='read-more' style='margin:15px 0;'>Read more</button>" );
//Set description container back to its original height
$( "header .read-more" ).on( "click", function() {
$("header .term-description").animate({height: curHeight});
});
});
});
</script>
<?php
}
Upvotes: 2