一個很常見的問題:有時候(主要是聯盟網上商店),WooCommerce 的產品標題太長了。除此之外,您還可能希望保持店面體驗的一致性,並使所有 WooCommerce 產品的標題長度相同。這是您怎麼做的

問題:WooCommerce 產品標題太長 – 我們可以在商店頁面設定字元長度限制

WooCommerce 。在商店頁面縮短產品標題

選項 1(CSS):僅將所有 WooCommerce 產品標題限制為一行

// Note: this is simple CSS that can be placed in your custom.css file // This CSS also adds 3 dots ... at the end of each product title  .woocommerce ul.products li.product h3 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } 

選項 2(PHP):將所有 WooCommerce 產品標題限制為最大字元數

// Note: this is simple PHP that can be placed in your functions.php file // Note: substr may give you problems, please check Option 3 add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 ); function shorten_woo_product_title( $title, $id ) { if ( is_shop() && get_post_type( $id ) === 'product' ) { return substr( $title, 0, 15 ); // change last number to the number of characters you want } else { return $title; } } 

選項 3(PHP):將所有 WooCommerce 產品標題限制為最大字數

謝謝 nicmare 這個片段!

function shorten_woo_product_title( $title, $id ) { if ( is_shop() && get_post_type( $id ) === 'product' ) { return wp_trim_words( $title, 4 ); // change last number to the number of WORDS you want } else { return $title; } } 


可以在哪裡新增此程式碼?

您可以將 PHP 程式碼片段放置在主題或子主題的 functions.php 檔案的底部(如果是 CSS 程式碼,請新增到主題的 style.css 檔案底部),修改之前建議先備份原始檔案,若出現錯誤請先刪除此程式碼。


這段程式碼是否正常可用?

或者是您有更好的解決方案想要分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 使用者也希望您的參與。


需要關於 WooCommerce 的幫助?

請觀看我們提供的免費影片教程或到薇曉朵 WooCommerce 中文論壇提問,會有專業技術團隊提供相關幫助。