簡體   English   中英

WooCommerce - 如何限制簡短的產品描述

[英]WooCommerce - How to limit the short product description

在 WooCommerce 中,如何限制商店頁面中的簡短產品描述? 我添加了這個代碼:

add_action('woocommerce_after_shop_loop_item_title','woocommerce_template_single_excerpt', 5);

但不能將其限制為 40 個字符。

謝謝!!!

而不是直接在插件中編輯文件(這是一個非常糟糕的主意,因為一旦更新插件,您的所有更改都將丟失!)

add_filter('woocommerce_short_description', 'limit_woocommerce_short_description', 10, 1);
function limit_woocommerce_short_description($post_excerpt){
    if (!is_product()) {
        $post_excerpt = substr($post_excerpt, 0, 20);
    }
    return $post_excerpt;
}

然后將其粘貼到主題的functions.php文件中。

並在要顯示產品說明的地方使用此行 -

<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ); ?> 

使用此代碼更改 Shop Page 而不是 Product-detailed Page 的限制

我會用:

function et_excerpt_length($length) {
    global $post;
    if($post->post_type=="product") return 40;
        return 20; /*Your default excerpt length*/
}
add_filter('excerpt_length', 'et_excerpt_length');

在 function.php 中添加它

您可以使用此代碼限制字數 -

add_filter('woocommerce_short_description', 'limit_woocommerce_short_description', 10, 1);
function limit_woocommerce_short_description($post_excerpt){
    if (!is_product()) {
        $pieces = explode(" ", $post_excerpt);
        $post_excerpt = implode(" ", array_splice($pieces, 0, 20));

    }
    return $post_excerpt;
}

explode將原始字符串分解為一個單詞數組,array_splice 可以讓您獲得這些單詞的某些范圍,然后implode將這些范圍組合成單個字符串。

使用此代碼更改商店頁面上的限制,而不是產品詳細頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM