【问题标题】:Reduce product long description in Woocommerce减少 Woocommerce 中的产品长描述
【发布时间】:2018-06-02 03:03:57
【问题描述】:

我找到了以下代码from this answer thread,但它只适用于产品标题下。那么如何才能适用于产品的详细描述呢。

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

【问题讨论】:

    标签: php wordpress woocommerce product hook-wordpress


    【解决方案1】:

    在 Woocommerce 产品单页中,长描述显示在“描述选项卡”中。如果你查看single-product/tabs/description.php模板的源代码,它使用the_content() wordpress 函数来显示那个长描述。

    所以你可以使用the_content专门的Wordpress过滤钩来减少产品长描述:

    add_filter( 'the_content', 'shorten_product_long_descrition', 20 );
    function shorten_product_long_descrition( $content ){
        // Only for single product pages
        if( ! is_product() ) return $content;
    
        // Set the limit of words
        $limit = 14;
    
        if (str_word_count($content, 0) > $limit) {
            $arr = str_word_count($content, 2);
            $pos = array_keys($arr);
            $text = '<p>' . substr($content, 0, $pos[$limit]) . '...</p>';
            $content = force_balance_tags($text); // needded
        }
        return $content;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    之前:

    之后:

    类似:Limit product short description length in Woocommerce

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多