久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

限制 Woocommerce 中的產品簡短描述長度

Limit product short description length in Woocommerce(限制 Woocommerce 中的產品簡短描述長度)
本文介紹了限制 Woocommerce 中的產品簡短描述長度的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我在我的 WordPress 網站上使用以下代碼來縮短我在 WooCommerce 上的描述摘錄,如果我輸入 14 個或更少的字符,它可以正常工作.只要我輸入超過 14 個字符,它就會顯示完整的簡短描述.

I'm using the following code on my WordPress site to shorten my description excerpt on WooCommerce and it works fine if I input my characters for 14 or less. As soon as I enter more than 14 characters it shows the full short description.

add_action( 'woocommerce_after_shop_loop_item_title', 'lk_woocommerce_product_excerpt', 35, 2);
if (!function_exists('lk_woocommerce_product_excerpt'))
{
    function lk_woocommerce_product_excerpt()
    {
        $content_length = 14;
        global $post;
        $content = $post->post_excerpt;
        $wordarray = explode(' ', $content, $content_length + 1);
        if(count($wordarray) > $content_length) :
            array_pop($wordarray);
            array_push($wordarray, '...');
            $content = implode(' ', $wordarray);
            $content = force_balance_tags($content);
            $content = substr($content, 0, 14);

        endif;
        echo "<span class='excerpt'><p>$content...</p></span>";
    }
}

任何幫助將不勝感激.

謝謝.

推薦答案

您的代碼正在計算帶有空格的字母,而下面的代碼正在計算沒有空格的單詞.請查看此實時 php 文件 (這里是你的代碼在包含 25 個單詞的字符串上的結果,我的也是).然后此代碼按您的意愿正常工作:

Your code is counting letters with white spaces, instead the code below is counting words without white spaces. Please See this live php file in action (here the result of your code on a string containing 25 words and mine too). Then this code is working correctly as you wish:

add_action( 'woocommerce_after_shop_loop_item_title', 'shorten_product_excerpt', 35 );
function shorten_product_excerpt()
{
    global $post;
    $limit = 14;
    $text = $post->post_excerpt;
    if (str_word_count($text, 0) > $limit) {
        $arr = str_word_count($text, 2);
        $pos = array_keys($arr);
        $text = substr($text, 0, $pos[$limit]) . '...';
        // $text = force_balance_tags($text); // may be you dont need this…
    }
    echo '<span class="excerpt"><p>' . $text . '</p></span>';
}

<小時>

或者您可以使用下面線程中的函數,以這種方式使用:


Or you can use the function from the thread below, with yours this way:

if (!function_exists('lk_limit_text'))
{
    function lk_limit_text($text, $limit) {
        if (str_word_count($text, 0) > $limit) {
            $words = str_word_count($text, 2);
            $pos = array_keys($words);
            $text = substr($text, 0, $pos[$limit]) . '...';
        }
        return $text;
    }
}

add_action( 'woocommerce_after_shop_loop_item_title', 'lk_woocommerce_product_excerpt', 35, 2);
if (!function_exists('lk_woocommerce_product_excerpt'))
{
    function lk_woocommerce_product_excerpt()
    {
        global $post;
        $content = $post->post_excerpt;
        // $content = force_balance_tags($content); // may be you dont need this…
        echo '<span class="excerpt"><p>' . lk_limit_text( $content, 14 ) . '</p></span>';
    }
}

這應該有效......

This should work…

此代碼基于此線程:如何將字符串截斷為 PHP 中的前 20 個單詞?

這篇關于限制 Woocommerce 中的產品簡短描述長度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 国产精品美女久久久久aⅴ国产馆 | 亚洲综合无码一区二区 | 亚洲精品亚洲人成人网 | 荷兰欧美一级毛片 | 精品国模一区二区三区欧美 | 久久精品亚洲一区二区三区浴池 | 99久久免费精品国产男女高不卡 | 午夜专区 | 在线视频一区二区 | 国产一区免费 | 国产在线一区二 | 日韩欧美中文字幕在线观看 | 国产精品区二区三区日本 | 国产日韩一区二区 | 国产精品欧美一区二区三区不卡 | 国产欧美一区二区精品久导航 | 91精品国产一区二区三区 | 国产精品国产精品国产专区不卡 | 国产女人叫床高潮大片免费 | 国产一区二区影院 | 精品中文字幕视频 | 久久久人成影片一区二区三区 | 精品成人在线视频 | 精品欧美一区二区三区久久久 | 国产激情在线 | 精品一区二区久久久久久久网站 | 精品一区二区三 | 99在线观看视频 | h小视频 | 国产一区日韩在线 | 欧美一区二区三区视频 | 在线国产视频 | 99久久精品国产麻豆演员表 | 久久精品国产亚洲 | 日韩精品一区二区三区四区视频 | 久久青草av | 久久精品视频一区二区三区 | 亚洲一区视频在线 | 国产精品免费av | 免费在线播放黄色 | 91精品久久久久久久久久入口 |