編碼:
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' && strlen( $title ) > 30 ) {
return substr( $title, 0, 30) . '…'; // change last number to the number of characters you want
} else {
return $title;
}
}
我知道我應該在wp_is_mobile()某個地方添加,但不知道我應該在哪里添加它。
uj5u.com熱心網友回復:
如果你想使用,wp_is_mobile()那么它只回傳一個布林值,所以你可以在任何包裝輸出的地方使用它:
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if ( wp_is_mobile() ) {
if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' && strlen( $title ) > 30 ) {
return substr( $title, 0, 30) . '…'; // change last number to the number of characters you want
} else {
return $title;
}
}
return $title;
}
但請記住,如果您使用頁面快取,移動用戶可能會生成快取,因此您的快取將包含更改并在任何地方顯示。鑒于這個特定的背景關系是 WooCommerce,因此如果您需要產品頁面以某種方式動態化,那么您可能不會快取產品頁面,這可能仍然有效,但如果快取是一個問題,上面的@markus-ao 評論將是一個更好的解決方案.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/530326.html
上一篇:如何使用JS/jQuery.animate({影片元素從Last到First/ReverseAnimation的函式
