【问题标题】:Limit product short description length in Woocommerce在 Woocommerce 中限制产品简短描述长度
【发布时间】:2016-07-02 01:06:04
【问题描述】:

我在我的 WordPress 网站上使用以下代码来缩短我在 WooCommerce 上的描述摘录,如果我输入 14 个或更少的字符,它可以正常工作。只要我输入超过 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>";
    }
}

任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: php wordpress woocommerce product hook-woocommerce


    【解决方案1】:

    您的代码是计算带有空格的字母,而不是下面的代码是计算没有空格的单词。请See this live php file in action (这里是你的代码在一个包含 25 个单词的字符串上的结果,我的也是)。那么这段代码就可以如你所愿地正常工作了:

    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>';
    }
    

    或者您可以使用下面线程中的函数,以这种方式使用您的函数:

    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>';
        }
    }
    

    这应该可行……

    此代码基于此线程:How can I truncate a string to the first 20 words in PHP?

    【讨论】:

    • 感谢 LoicTheAztec 代码!不幸的是,这根本无法缩短描述长度。您还有其他想法为什么原始代码无法超过 14 个字符?我尝试了不同的 WordPress 主题,但没有任何区别。
    • 出于某种原因,我认为您的原始代码是在计算字符,而不是单词。所以它确实有效!非常感谢!
    • 只是一个快速跟进的问题。有没有一种简单的方法来更改代码,使其使用 WooCommerce 完整描述而不是简短描述?
    • @user3612498 是的,当然,您可以将$post-&gt;post_excerpt; 替换为$post-&gt;post_content;...
    • 感谢@LoicTheAztec,你是最棒的!
    【解决方案2】:

    您可以限制 WooCommerce 产品描述

    使用此代码 -

    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 将原始字符串分解为单词数组,array_splice 让您获得这些单词的特定范围,然后 implode 将这些范围重新组合成单​​个字符串。

    使用此代码更改商店页面而非产品详细页面上的限制

    如果您想在两个页面上进行更改,请删除 if (!is_product()) 条件。

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 2020-10-21
      • 1970-01-01
      • 2016-06-12
      • 2016-07-10
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 2018-08-14
      相关资源
      最近更新 更多