Add shortened description under the product title in WooCommerce archive pages(在 WooCommerce 存档页面的产品标题下添加简短的描述)
问题描述
在 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 存档页面的产品标题下添加简短的描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 WooCommerce 存档页面的产品标题下添加简短的描


基础教程推荐
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- php中的PDF导出 2022-01-01
- php中的foreach复选框POST 2021-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01