我正在尝试在产品类别页面上限制woocommerce中产品类别描述的字数,并添加一个阅读更多链接来扩展文本。我一直在尝试编辑产品描述字符限制,但没有成功。任何帮助都将不胜感激。下面是我在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>';
}发布于 2020-07-18 18:43:15
您可以使用一些jQuery来缩小描述容器的高度,并添加一个“阅读更多”按钮,该按钮将把容器的大小调整回其原始高度。
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
}发布于 2021-04-14 15:52:49
这就是我在我的网站上实现的方式。谢谢终结者-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();
});
}https://stackoverflow.com/questions/62952907
复制相似问题