首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Woocommerce限制字数统计产品类别说明文本

问Woocommerce限制字数统计产品类别说明文本
EN

Stack Overflow用户
提问于 2020-07-17 19:21:08
回答 2查看 667关注 0票数 4

我正在尝试在产品类别页面上限制woocommerce中产品类别描述的字数,并添加一个阅读更多链接来扩展文本。我一直在尝试编辑产品描述字符限制,但没有成功。任何帮助都将不胜感激。下面是我在functions.php中编辑的代码:

代码语言:javascript
复制
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>';
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-18 18:43:15

您可以使用一些jQuery来缩小描述容器的高度,并添加一个“阅读更多”按钮,该按钮将把容器的大小调整回其原始高度。

代码语言:javascript
复制
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
}
票数 2
EN

Stack Overflow用户

发布于 2021-04-14 15:52:49

这就是我在我的网站上实现的方式。谢谢终结者-barbapapa

代码语言:javascript
复制
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();
            });    
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62952907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档