Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save assoscoupa/5fbbae8510681b761785c26226bbb732 to your computer and use it in GitHub Desktop.
Save assoscoupa/5fbbae8510681b761785c26226bbb732 to your computer and use it in GitHub Desktop.
Add Read More in WooCommerce Category Description
add_action( 'woocommerce_after_main_content', 'qwerty_gr_woocommerce_after_main_content' );
function qwerty_gr_woocommerce_after_main_content() {
if ( is_product_category() ){
wc_enqueue_js('
var show_char = 60;
var ellipses = "... ";
var content = $(".term-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 class=\'read-more\'>Read more</a></span><span class=\'truncated\' style=\'display:none\'>" + b + "</span>";
$(".term-description").html(html);
}
$(".read-more").click(function(e) {
e.preventDefault();
$(".term-description .truncated").toggle();
});
');
}
}
@assoscoupa
Copy link
Author

assoscoupa commented Jun 4, 2021

// Alternative Method

add_action( 'wp_head', function () { ?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>

jQuery(function() {

  var minimized_elements = $('div.term-description');

  minimized_elements.each(function() {
    var t = $(this).text();
    if (t.length < 100) return;

    $(this).append('<a href="#" class="less">Show Less</a>')
    $(this).wrapInner('<div class="original"></div>');

    $(this).append('<div class="intro"><p>' +
      t.slice(0, 100) +
      '<span>... </span><a href="#" class="more">Read More</a></p></div>'
    );

    $(this).find('.original').hide();
  });

  $('a.more', minimized_elements).click(function(event) {
    event.preventDefault();
    $(this).closest('.intro').hide().prev('.original').show();
  });

  $('a.less', minimized_elements).click(function(event) {
    event.preventDefault();
    $(this).closest('.original').hide().next('.intro').show();

  });

});

</script>
<?php } );

@merckx84
Copy link

FYI the Alternative Method is very clean and has the read less button, but unfortunately it breaks Ajax sorting and add to cart on woocomerce pages (did on my theme)

Is there a way to add read less to the '[Add_Read_More_in_WooCommerce_Category_Description.php]'?

Thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment