mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
https://businessbloomer.com/woocommerce-truncate-short-description-with-read-more-toggle/
30 lines
1.1 KiB
Text
30 lines
1.1 KiB
Text
add_action( 'wp_footer', 'wc_woocommerce_short_description_truncate_read_more' );
|
|
|
|
function wc_woocommerce_short_description_truncate_read_more() {
|
|
if ( ! is_product() ) return;
|
|
?>
|
|
<script type="text/javascript">
|
|
|
|
jQuery(document).ready(function($){
|
|
|
|
var show_char = 40;
|
|
var ellipses = "... ";
|
|
var content = $(".woocommerce-product-details__short-description").html();
|
|
|
|
if (content.length > show_char) {
|
|
var a = content.substr(0, show_char);
|
|
var b = content.substr(show_char - content.length);
|
|
var html = a + '<span class="truncated">' + ellipses + '<a href="" class="read-more">Read more</a></span><span class="truncated" style="display:none">' + b + '</span>';
|
|
$(".woocommerce-product-details__short-description").html(html);
|
|
}
|
|
|
|
$(".read-more").click(function(e) {
|
|
e.preventDefault();
|
|
$(".woocommerce-product-details__short-description .truncated").toggle();
|
|
});
|
|
|
|
});
|
|
|
|
</script>
|
|
<?php
|
|
}
|