問題描述
在 Woocommerce 檔案頁面中,我應該需要在每個產品標題下顯示產品描述中的幾行,如本圖所示.
In Woocommerce archives pages, I should need to display couple of lines from the product description under each product title like it's shown in this image.
我該怎么做?
推薦答案
更新: 此自定義功能將縮短產品描述(到定義的字數)并將其顯示在存檔頁面中的每個產品作為商店:
Updated: This custom function will shorten the product description (to a defined amount of words) and will display it under the title of each product in archive pages as shop:
add_action('woocommerce_after_shop_loop_item_title', 'description_in_shop_loop_item', 3 );
function description_in_shop_loop_item() {
global $product;
// HERE define the number of words
$limit = 10;
$description = $product->get_description(); // Product description
// or
// $description = $product->get_short_description(); // Product short description
// Limit the words length
if (str_word_count($description, 0) > $limit) {
$words = str_word_count($description, 2);
$pos = array_keys($words);
$excerpt = substr($description, 0, $pos[$limit]) . '...';
} else {
$excerpt = $description;
}
echo '<p class="description">'.$excerpt.'</p>';
}
代碼位于活動子主題(或活動主題)的 function.php 文件中.
Code goes in function.php file of your active child theme (or active theme).
或者您可以根據字符長度限制使用相同的內容:
Or you can have the same thing based on characters length limit:
add_action('woocommerce_after_shop_loop_item_title', 'description_in_shop_loop_item', 3 );
function description_in_shop_loop_item() {
global $product;
// HERE define the number of characters
$limit = 75;
$description = $product->get_description(); // Product description
// or
// $description = $product->get_short_description(); // Product short description
// Limit the characters length
if (strlen($description) > $limit) {
$excerpt = substr($description, 0, $limit) . '...';
} else {
$excerpt = $description;
}
echo '<p class="description">'.$excerpt.'</p>';
}
代碼位于活動子主題(或活動主題)的 function.php 文件中.
Code goes in function.php file of your active child theme (or active theme).
這兩個功能都經過測試和工作.
Both functions are tested and works.
這篇關于在 WooCommerce 存檔頁面的產品標題下添加簡短的描述的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!