この回答はタイトル長と語長"、単語を壊すのを避けるために。
部分的this answer上とwoocommerce_template_loop_product_title()
WooCommerceネイティブ関数に基づいて、この関数は、それはショップページにタイトルを表示するにはcontent-product.php WooCommerceテンプレート、に使用されています。ここで
私はあなたの制限文字列の長さが含まれているが、また、ブレイクの言葉を避けるために、複雑な「の単語の長さ」検出に基づいています:
if ( ! function_exists('woocommerce_template_loop_product_title')) {
// Show the product title in the product loop. By default this is an <h3> html tag.
function woocommerce_template_loop_product_title() {
// Define the lenght limit for title (by line)
$limit = 29;
$title = get_the_title();
$lenght = strlen($title);
// 1. The title length is higher than limit
if ($lenght >= $limit) {
$title_arr1 = array();
$title_arr2 = array();
$sum_length_words = -1;
// an array of the words of the title
$title_word_arr = explode(' ', $title);
// iterate each word in the title
foreach($title_word_arr as $word){
// Length of current word (+1 space)
$length_word = strlen($word) + 1;
// Adding the current word lenght to total words lenght
$sum_length_words += $length_word;
// Separating title in 2 arrays of words depending on lenght limit
if ($sum_length_words <= $limit)
$title_arr1[] .= $word;
else
$title_arr2[] .= $word;
}
// Converting each array in a string
$splitted_title = implode(" ", $title_arr1). ' ('. strlen(implode(" ", $title_arr1)) .')';
$splitted_title .= '<br>'; // adding <br> between the 2 string
$splitted_title .= implode(" ", $title_arr2). ' ('. strlen(implode(" ", $title_arr2)) .')';
echo '<h3>' . $splitted_title . '</h3>';
// 2. The title length is NOT higher than limit
} else {
echo '<h3>' . $title . '</h3>';
}
}
}
このコードは、関数になります。あなたのアクティブな子テーマ(またはテーマ)の、または任意のプラグインファイルのphpファイル。
このコードはテスト済みであり、動作します。
私は元々どのように意図していたのですか? 私はいくつかの製品名を短絡しているように、実際にはユーザーにとって理にかなっていません –