WordPressで記事の抜粋を作成する改変版

先日作成した記事抜粋の関数「tkb_get_excerpt_ja」ですが、ちょっと修正したので再アップします。

  • 記事取得をget_the_contentから$postに変更
  • 記事を省略した場合の付与文字列を変数にして変更しやすく
  • 省略を表示する文字列に記事へのリンクを作成

Usage

<?php $excerpt = tkb_get_excerpt_ja($cnt_textlen, $my_content) ?>

Parameters

$cnt_textlen (integer)
取り出したい文字数を入力します。記載なしの場合は100文字
$my_content (string)
抽出元の文章です。無記入だとget_the_content()を取ります

Examples

いちばん簡単な使い方はループ内に下記のように書いてあげればOKです。

<?php echo tkb_get_excerpt_ja() ?>

150文字とりたい場合は数字を入れる。

<?php echo tkb_get_excerpt_ja(150) ?>

Description

ソースをfunctions.phpに貼り付けてご利用ください。
特徴としては文末判定用の文字を使い、センテンス単位で抽出します。なので文末がキレイに終わります。
※実際には do〜while の判定で $cnt_textlen + 15 としているので、この15文字に入ると文章が途中で切れます。ここは好きなように加減してください。
あとは抜粋に不要なタグとその他いろいろ削除しています。
見出し・テーブル・引用・コード・追記・caption(WordPress用)・URL表記・余分な改行と空白
[全文表示]の部分を変えるには$txt_moreを書き換えます。
$txt_more_linkに”1″以外の文字を入れるとリンクを作成しません。※文字は表示されます。

Source

function tkb_get_excerpt_ja($cnt_textlen = 100, $my_content = "") {
    global $post;
    if ($my_content==""){ $my_content = ($post->post_content); }

    $cnt_sentence = ceil($cnt_textlen/30); //読みやすい1sentenceの平均文字数は33〜35文字らしい
    $ptn_sentenceend = "(。|?|!|♪|)\\n|\\)\\n)"; //文末判定文字-正規表現
    $txt_more = "[全文表示]"; //記事省略時の文字列
    $txt_more_link = "1"; //1なら省略表示文字列に記事へのリンク

    $my_content = preg_replace("/[\r\n|\r]/", "\n", $my_content); //改行コード統一
    $my_content = preg_replace("/<h[1-6].+?\/h[1-6]>/si", "", $my_content); //hxを中身ごと削除
    $my_content = preg_replace("/<table.+?\/table>/si", "", $my_content); //tableを中身ごと削除
    $my_content = preg_replace("/<blockquote.+?\/blockquote>/si", "", $my_content); //blockquoteを中身ごと削除
    $my_content = preg_replace("/<pre.+?\/pre>/si", "", $my_content); //preを中身ごと削除
    $my_content = preg_replace("/<ins.+?\/ins>/si", "", $my_content); //insを中身ごと削除
    $my_content = preg_replace("/\[caption.+?](.+?)\[\/caption]/si", "$1", $my_content); //captionを中身残して削除
    $my_content = preg_replace("/(?:http|https):\/\/[-_.!~*()a-zA-Z0-9;\/?:@&=+$,%#]+/i", "", $my_content); //URLを削除
    $my_content = strip_tags($my_content); //タグを削除
    $my_content = preg_replace("/\n+/", "\n", $my_content); //空行削除

    if ($txt_more_link==1){
    $txt_more = '<a href="' . get_permalink() . '" >' . $txt_more . '</a>';
    }

    if (mb_strlen($my_content) > $cnt_textlen){ //現在の状態で100文字なければそのまま出力
        $i = $cnt_sentence; do {
            $my_pattern = "^(.+?".$ptn_sentenceend."){".$i."}";
            $retval = preg_match("/$my_pattern/si", $my_content, $my_preg); //文末判定
            $i = $i - 1;
        } while ($i > 1 and ( mb_strlen($my_preg[0]) > $cnt_textlen + 15 || $my_preg[0] == null ));
        if ($my_preg != null){ if (mb_strlen($my_preg[0]) > $cnt_textlen){
            $ret_content = mb_substr($my_preg[0], 0, $cnt_textlen)."...".$txt_more;
            } else { $ret_content = $my_preg[0].$txt_more; }
        } else { $ret_content = mb_substr($my_content, 0, $cnt_textlen)."...".$txt_more; } 
    } else { $ret_content = $my_content; }

    $ret_content = preg_replace("/\s+/m", " ", $ret_content); //連続するスペースを1つに
    return $ret_content;
}

Notes

投稿場所(index/search等とsingleページ)によらず全文から抽出するように統一しました。
これによって<!–more–>タグはいっさいムシします。
あとはmore表示をゴソゴソと。関数の引数にしてもよかったのですが、それも使いにくくなるかと考えfunction内に記載しています。変更する場合は直接書き換えてください。
各エントリーに抜粋データがあっても完全無視なのは仕様です。

Notice: Undefined offset: 0 が表示される場合はスクリプトの先頭に下記を追記してください。
これは配列で存在しないインデックス($my_preg[0])を参照したときに発生します。
error_reporting(E_ALL & ~E_NOTICE);

これでまぁなんとかカタチにはなったかな。

Related Links – I’m really grateful for everything.

Function Reference / get the excerpt | WordPress Codex
Returns the post excerpt for assignment to a variable.
WP Multibyte Patch | EastCoder;
WordPressのマルチバイト文字の取り扱いに関する不具合の累積的修正と強化を行うプラグインです。

「WordPressで記事の抜粋を作成する改変版」への1件のフィードバック

コメントは停止中です。