限制Woocommerce中的产品简短描述长度 [英] Limit product short description length in Woocommerce

查看:221
本文介绍了限制Woocommerce中的产品简短描述长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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>";
    }
}

任何帮助将不胜感激.

Any help would be appreciated.

谢谢.

推荐答案

您的代码正在计算带有空格的字母,而下面的代码正在计算没有空格的单词.请> 查看此实时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 code is based on this thread: How can I truncate a string to the first 20 words in PHP?

这篇关于限制Woocommerce中的产品简短描述长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆