MAKIZOU.COM

WEB系SEのホームページ作成&サーバ構築運用メモ

WordPress ME2.1.2 - the_excerptタグの日本語対応

the_excerpt()は、記事の概要欄に書いていない場合は、HTMLタグが除去され、55文字(英語の場合)で省略される表示する機能があります。

これが、日本語(マルチバイト文字)の場合には、困った事にHTMLタグが除去された全ての文字が表示されるようです。

んで、全部表示は嫌だったので、PHPプログラム内のユーザー関数に手を加える事にしました。
その、手を加えた部分をメモっておきます。

変更後の
$excerpt_length = 100;
は日本語(マルチバイト文字)で、何文字で省略するかを指定します。
この場合は100文字で省略しています。

ファイル名(PATH)
wp-includes/formatting.php
ユーザー関数名
wp_trim_excerpt

変更前

function wp_trim_excerpt($text) { // Fakes an excerpt if needed
	global $post;
	if ( '' == $text ) {
		$text = get_the_content('');
		$text = apply_filters('the_content', $text);
		$text = str_replace(']]>', ']]>', $text);
		$text = strip_tags($text);
		$excerpt_length = 55;
		$words = explode(' ', $text, $excerpt_length + 1);
		if (count($words) > $excerpt_length) {
			array_pop($words);
			array_push($words, '[...]');
			$text = implode(' ', $words);
		}
	}
	return $text;
}

変更後

function wp_trim_excerpt($text) { // Fakes an excerpt if needed
	global $post;
	if ( '' == $text ) {
		$text = get_the_content('');
		$text = apply_filters('the_content', $text);
		$text = str_replace(']]>', ']]>', $text);
		$text = strip_tags($text);
/*
		$excerpt_length = 55;
		$words = explode(' ', $text, $excerpt_length + 1);
		if (count($words) > $excerpt_length) {
			array_pop($words);
			array_push($words, '[...]');
			$text = implode(' ', $words);
*/
		$excerpt_length = 100;
		if (mb_strlen($text)> $excerpt_length) {
			$text = mb_substr(
				$text, 0, $excerpt_length, mb_detect_encoding($text)
			) . "...";
		}
	}
	return $text;
}

なお、プログラムの修正は自己責任でお願いいたします。
(^o^)/

  • カテゴリ:Web開発
  • 公開日:
  • 1つ星2つ星3つ星4つ星5つ星
    Loading ... Loading ...
    ↑ 記事評価をお願いします。

コメント&トラックバック

トラックバック用URL

コメント




使用できるXHTMLタグ <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>