カテゴリー別アーカイブ: 未分類

海外テーマの文字化けを回避する

文字を短く区切った部分だけ、文字化けしちゃうのよね〜
マルチバイトのプラグインは有効化してるのに、
文末が◆に?の文字が入る所と入らない所がある。

と思って調べたら下記のサイトを発見!
http://zatzcan.com/wordpress/26.html

「function.php」や「tabs.php」などから以下のような記述を見つけ、
2バイト文字に対応したmb_substrに変更とのこと。

■修正前

$text = substr($text,0,$chars_limit);
$text = substr($text,0,strrpos($text,' '));

■修正後

$text = mb_substr($text,0,$chars_limit,'UTF-8');
$text = mb_substr($text,0,strrpos($text,' '),'UTF-8');

とはいえ、だいぶ書いてあるコードが違ったかも、と思ってメモメモ!

「functions.php」内の記述を修正

■修正前

function print_excerpt($length) {
	global $post;
	$text = $post->post_excerpt;
	if ( '' == $text ) {
		$text = get_the_content('');
		$text = apply_filters('the_content', $text);
		$text = str_replace(']]>', ']]>', $text);
	}
	$text = strip_shortcodes($text); 
	$text = strip_tags($text);

	$text = substr($text,0,$length);
	$excerpt = reverse_strrchr($text, '.', 1);
	if( $excerpt ) {
		echo apply_filters('the_excerpt',$excerpt);
	} else {
		echo apply_filters('the_excerpt',$text);
	}
}

function reverse_strrchr($haystack, $needle, $trail) {
    return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
}

■修正後

function print_excerpt($length) {
	global $post;
	$text = $post->post_excerpt;
	if ( '' == $text ) {
		$text = get_the_content('');
		$text = apply_filters('the_content',
		$text); $text = str_replace(']]>', ']]>', $text);
	}
	$text = strip_shortcodes($text);
	$text = strip_tags($text);

	$text = mb_substr($text,0,$length,'UTF-8');
	$excerpt = reverse_strrchr($text, '.', 1);
	if( $excerpt ) {
		echo apply_filters('the_excerpt',$excerpt);
	} else {
		echo apply_filters('the_excerpt',$text);
	}
}

function reverse_strrchr($haystack, $needle, $trail) {
	return strrpos($haystack, $needle) ? mb_substr($haystack, 0, strrpos($haystack, $needle) + $trail,'UTF-8') : false;
}

ふ〜。長かった。
「substr」を「mb_substr」にして、’UTF-8’を追加するのを2箇所づつやっただけですが。
とりあえず、忘れないように・・・