简体   繁体   English

限制 Woocommerce 中的产品简短描述长度

[英]Limit product short description length in Woocommerce

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.我在我的 WordPress 网站上使用以下代码来缩短我在 WooCommerce 上的描述摘录,如果我输入 14 个或更少的字符,它可以正常工作。 As soon as I enter more than 14 characters it shows the full short description.只要我输入超过 14 个字符,它就会显示完整的简短描述。

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.任何帮助将不胜感激。

Thank you.谢谢。

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) .查看这个实时 php 文件(这里是包含 25 个单词的字符串的代码结果,我的也是) 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…这应该有效……

This code is based on this thread: How can I truncate a string to the first 20 words in PHP?此代码基于此线程: How can I truncate a string to the first 20 words in PHP?

You can Limit WooCommerce product description您可以限制 WooCommerce 产品描述

use this code -使用此代码 -

add_filter('woocommerce_short_description', 'limit_product_short_description', 10, 1);

function limit_product_short_description($post_excerpt)
 {
    if (!is_product()) 
    {
         $pieces = explode(" ", $post_excerpt);
         $post_excerpt = implode(" ", array_splice($pieces, 0, 14));
     }
   return $post_excerpt;
 }

explode breaks the original string into an array of words, array_splice lets you get certain ranges of those words, and then implode combines the ranges back together into single strings. explode将原始字符串分解为一个单词数组,array_splice 可以让您获得这些单词的某些范围,然后implode将这些范围组合成单个字符串。

Use this code to change Limit on the Shop Page not Product-detailed Page .使用此代码更改 Shop Page 而不是 Product-detailed Page 的限制

If you want to change on both pages remove if (!is_product()) condition.如果要在两个页面上更改,请删除if (!is_product())条件。

I want the same but only for the related products. 我想要相同的商品,但仅适用于相关产品。 A solution please ? 一个解决方案好吗?

Sorry for my english, i'm french. 对不起,我的英语,我是法语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM