WordPressでタイトルの文字数を制限してX文字以上を…にする方法
フォーラムに記載されていた方法
function.phpに以下を記載
function trim_str_by_chars( $str, $len, $echo = true, $suffix = '…', $encoding = 'UTF-8' ) {
if ( ! function_exists( 'mb_substr' ) || ! function_exists( 'mb_strlen' ) ) {
return $str;
}
$len = (int)$len;
if ( mb_strlen( $str = wp_specialchars_decode( strip_tags( $str ), ENT_QUOTES, $encoding ), $encoding ) > $len ) {
$str = wp_specialchars( mb_substr( $str, 0, $len, $encoding ) . $suffix );
}
if ( $echo ) {
echo $str;
} else {
return $str;
}
}
タイトル出力箇所に以下を記載
<?php trim_str_by_chars( get_the_title(), 10 ); ?>
10…が切り取る文字数。
ただ、半角・全角問わず1文字とカウントしている模様。
http://ja.forums.wordpress.org/topic/1398