「the_excerpt()」でも抜粋の文字数変更が効かない対処方法

「the_excerpt()」の抜粋の文字数を変える記述をfuncution.phpに書いても「110文字」勝手に抜粋されてしまったので調べてみました。

「WP Multibyte Patch」プラグインが原因

wordpressを日本語環境に対応させるプラグイン「WP Multibyte Patch」が入ってるとうまくいかないみたい。

なので「add_filter」の部分に「excerpt_mblength」を設定する必要性があります。

function my_excerpt_length($length) {
  return 60;
}
add_filter('excerpt_mblength', 'my_excerpt_length');

文字数を変更するだけであれば以上です。文字数を変更して抜粋記事の最後を変更する場合は以下を参考にしてどうぞ

抜粋文字数・末尾変更して出力する方法

// 記事抜粋文字数制限
function my_excerpt_length($length) {
  return 60;
}
add_filter('excerpt_mblength', 'my_excerpt_length');

// 抜粋末尾の省略文字変更
function my_excerpt_more($more) {
  return '…';
}
add_filter('excerpt_more', 'my_excerpt_more');

記事抜粋の文字数を60文字に変更して、末尾(デフォだと「[…]」)の文字を「…」に変更しています。

固定ページテンプレート (page-cms.php)

// 抜粋記事の出力方法
<?php echo get_the_excerpt(); ?>

「the_excerpt();」は出力時pタグが一緒に出力されるの「get_the_excerpt()」で出力しています。

●参考サイト
【WordPress】「the_excerpt()」で抜粋の文字数変更が効かない件 | web関連 | 二色人日記。