抜粋モドキ小細工

Wordpress には the_excerpt っていう記事の抜粋を表示するテンプレートタグがあるけど、わざわざ抜粋欄に概要を入力することなんて滅多に無い。
っちゅーか、日記なんで概要もヘチマも無いんですわ。
そんでこのテンプレートタグは、抜粋欄に入力されたデータが無い場合は全文表示(日本語の場合)されるのよね。

そんなんやだ〜。。。

てことでちと考えた2007.01.21 phpリファレンスマニュアル買って考えた
こんなんどうや?

echo mb_substr(strip_tags($post->post_content),0,100)

echo mb_strimwidth(strip_tags($post->post_content),0,150,’… ’)

やってることは本文から HTML タグを削除して、先頭からマルチバイト100文字半角換算150文字を切り出してる。
まぁまぁ使えるやんけ〜。
・・・まぁまぁってのは、HTML タグの状況によっては最初の数文字しか出てこないことがあんのよ。(^^;
まぁ、抜粋ってことでエエことにしましょ。
てことで採用!

ちなみに現在のところ year と search と tag の時にこの抜粋モドキで閲覧できます。

Search Excerpt Plugin へ組み込み

上記の小細工は Search Excerpt Plugin 1.2 へ組み込み、the_excerpt で出力するようにしました。
改造箇所は160〜180行目にある function the_excerpt の部分を下記に書き換え。

[code lang="php"]
function the_excerpt($text) {
static $filter_deactivated = false;
global $more;
global $wp_query;

$more = 1;
remove_filter('the_excerpt', 'wpautop');
$filter_deactivated = true;
$content = SearchExcerpt::get_content();

if (!is_search()) { // If we are not in a search - simply return the text unmodified.
$content = strip_tags($content);
$content = mb_strimwidth($content,0,150,'... ');
return $content;

} else { // Get the whole document, not just the teaser.
$query = SearchExcerpt::get_query($wp_query->query_vars['s']);
return SearchExcerpt::highlight_excerpt($query, $content);
}
}
?>
[/code]
はぁ、なんかすっきりしたな。。。