0

私はWordpress内で開発していますが、PHPの知識は最小限です。タイトルには最初の単語しか表示されていません。変更するにはどうすればよいですか?これは私がそれが最初の言葉に制限されていると信じているところです。

function ShortenTitle($title){
// Change to the number of characters you want to display
$chars_max = 100;
$chars_text = strlen($title);
$title = $title."";
$title = substr($title,0,$chars_max);
$title = substr($title,0,strrpos($title,' '));
if ($chars_title > $chars_max)
{
$title = $title."...";
}
return $title;
}
function limit_content($str, $length) {
  $str = strip_tags($str);
  $str = explode(" ", $str);
  return implode(" " , array_slice($str, 0, $length));
}
4

2 に答える 2

1

このtrim関数は文字列の末尾から空白を削除します。これがあなたがこれでやろうとしていることだと思います

$title = substr($title,0,strrpos($title,' '));

また、安全/正確のために、長さを計算する前にトリミングする必要があります。これを試して:

function ShortenTitle($title){
    // Change to the number of characters you want to display
    $chars_max = 100;
    $new_title = substr(trim($title),0,$chars_max);
    if (strlen($title) > strlen($new_title)) {
        $new_title .= "...";
    }
    return $new_title;
}
于 2013-01-11T16:16:23.373 に答える
1

if ($chars_title > $chars_max)する必要がありますif ($chars_text > $chars_max)

これを試して:

function ShortenTitle($title) {
    $title = trim($title);
    $chars_max = 100;
    if (strlen($title) > $chars_max) {
        $title = substr($title, 0, $chars_max) . "...";
    }
    return $title;
}

少しきれいにしました。

于 2013-01-11T16:13:56.470 に答える