2017-02-12 23 views
1

我想用長描述替換產品的摘錄。現在我使用下面的代碼:顯示說明而不是摘錄,限制字詞並添加閱讀更多

remove_action('woocommerce_single_product_summary',  
'woocommerce_template_single_excerpt', 20); 
add_action('woocommerce_single_product_summary', 'the_content', 10); 

上面的代碼完成這項工作,但它顯示了完整的描述。我想以某種方式限制顯示的單詞(長度),並在末尾添加「閱讀更多」按鈕。

+2

長描述不支持閱讀更多的產品。由於我們在標籤上顯示內容,因此沒有用處。 – Yasir

回答

1

只需創建一個新的函數來處理get_the_content的值(),以獲得唯一的話的最大數量,並在末尾添加了「更多」鏈接:

function custom_single_product_summary(){ 
    $maxWords = 50; // Change this to your preferences 
    $description = strip_tags(get_the_content()); // Remove HTML to get the plain text 
    $words = explode(' ', $description); 
    $trimmedWords = array_slice($words, 0, $maxWords); 
    $trimmedText = join(' ', $trimmedWords); 

    if(strlen($trimmedText) < strlen($description)){ 
    $trimmedText .= ' &mdash; <a href="' . get_permalink() . '">Read More</a>'; 
    } 

    echo $trimmedText; 
} 

然後在原來的使用重寫代碼,你試圖使用:

remove_action('woocommerce_single_product_summary',  
'woocommerce_template_single_excerpt', 20); 
add_action('woocommerce_single_product_summary', 'custom_single_product_summary', 10); 

修訂答: 改變了行動鉤來呼應VALU因爲WooCommerce期望採取行動來打印輸出。

+0

感謝您的回覆。但是,該產品的「摘錄」部分仍未顯示任何內容。 – user2093301

+0

請參閱我的更新回答。該動作需要回顯輸出,而不是從函數返回,以使其工作。 – ablopez

相關問題