php Woocommerce限制字数产品类别描述文本

jvidinwx  于 2023-01-01  发布在  PHP
关注(0)|答案(2)|浏览(180)

我正在尝试限制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>';
}
zbq4xfa0

zbq4xfa01#

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

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
}
wgx48brx

wgx48brx2#

这是我如何在我的网站上实现.谢谢终结者-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();
            });    
    }

相关问题