wordpress 抜粋文字幅を自在に取得する方法です。PHP に用意されている関数 mb_strimwidth を用います。(スタイルシートで text-overflow 属性を指定することで対応できますが、ブラウザにより対応していないことがあります。)
(wordpress 3.3.1 の場合)投稿抜粋は単語数ベースで作成されます。本文に日本語等のマルチバイトを使う場合、適切にカウントされず、ほぼ全文が抜粋に入ることが多いです。逆に言えば、表示するときに抜粋の長さを指定できる(データベースに変更を加えずにカスタマイズできる)、ということでもあります。
抜粋文字幅を自在に取得する方法は、下記コードを functions.php に追加し、ループ内で、my_excerpt と記述すれば OK です。(記号だけの抜粋は文字コードチェックで跳ねます)
function my_excerpt($length=110,$moretext="...") {
echo _get_excerpt_mbstrimwidth($length,$moretext,get_the_excerpt());
}
function _get_excerpt_mbstrimwidth($length=110,$moretext="...",$content="") {
if ("UTF-8" != mb_detect_encoding($content) ) {
return false;
}
$length = intval($length);
$moretext = strip_tags($moretext);
$outdata = mb_strimwidth($content,0,$length,$moretext,"UTF-8");
$outdata = htmlspecialchars($outdata,ENT_QUOTES,"UTF-8");
return $outdata;
}
抜粋の長さ(幅)を指定できます。半角=1、全角=2とカウントされるので注意してください。また末尾の省略文字も指定できます。my_excerpt([長さ], [末尾の省略文字]) のように、オプション引数を追加してください。
PHPUnit を使ったテストコードは下記です(文字コードは UTF-8 決め打ち)。
require_once('my_excerpt.php');
if( !function_exists('get_the_content')){
function get_the_content() {
return TRUE;
}
}
class MyExcerptTest extends PHPUnit_Framework_TestCase {
private $ba;
public function setUp() {
}
public function test110len() {
$input = 'あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえお';
$output = _get_excerpt_mbstrimwidth(110,"...",$input);
$expected = $input;
$this->assertEquals($expected, $output);
}
public function test112len() {
$input = 'あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおか';
$output = _get_excerpt_mbstrimwidth(110,"...",$input);
$expected = 'あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいう...';
$this->assertEquals($expected, $output);
}
public function test10len() {
$input = 'あいうえお';
$output = _get_excerpt_mbstrimwidth(10,"...",$input);
$expected = $input;
$this->assertEquals($expected, $output);
}
public function test12len() {
$input = 'あいうえおか';
$output = _get_excerpt_mbstrimwidth(10,"...",$input);
$expected = 'あいう...';
$this->assertEquals($expected, $output);
}
public function testNotUTF() {
$input = '==========================';
$output = _get_excerpt_mbstrimwidth(10,"...",$input);
$expected = false;
$this->assertEquals($expected, $output);
}
public function test_reject_script() {
$input = 'あい<script>alert</script>';
$output = _get_excerpt_mbstrimwidth(110,"...",$input);
$expected = 'あい<script>alert</script>';
$this->assertEquals($expected, $output);
}
public function test_more() {
$input = 'あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおか';
$output = _get_excerpt_mbstrimwidth(110,"--続く--",$input);
$expected = 'あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あ--続く--';
$this->assertEquals($expected, $output);
}
public function test_more2() {
$input = 'あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおか';
$output = _get_excerpt_mbstrimwidth(110,"--------",$input);
$expected = 'あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あ--------';
$this->assertEquals($expected, $output);
}
public function test_more_reject_tag() {
$input = 'あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおか';
$output = _get_excerpt_mbstrimwidth(110,"<b>abc</b>",$input);
$expected = 'あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうえおかきくけ十あいうabc';
$this->assertEquals($expected, $output);
}
}

[…] wordpress 抜粋文字幅を自在に取得するも参考にしてください。 […]