在 WooCommerce 存档页面的产品标题下添加简短描述
Add shortened description under the product title in WooCommerce archive pages
在 Woocommerce 存档页面中,我应该需要在每个产品标题下显示产品描述中的几行,如图所示。
我该怎么做?
更新:此自定义函数将缩短产品描述(到定义的字数)并将其显示在存档页面中每个产品的标题下,如商店:
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>';
}
代码进入您的活动 child 主题(或活动主题)的 function.php 文件。
或者你可以根据字符长度限制做同样的事情:
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>';
}
代码进入您的活动 child 主题(或活动主题)的 function.php 文件。
这两个功能都经过测试并且有效。
在 Woocommerce 存档页面中,我应该需要在每个产品标题下显示产品描述中的几行,如图所示。
我该怎么做?
更新:此自定义函数将缩短产品描述(到定义的字数)并将其显示在存档页面中每个产品的标题下,如商店:
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>';
}
代码进入您的活动 child 主题(或活动主题)的 function.php 文件。
或者你可以根据字符长度限制做同样的事情:
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>';
}
代码进入您的活动 child 主题(或活动主题)的 function.php 文件。
这两个功能都经过测试并且有效。