WordPressで記事の抜粋を取得してくれる get_the_excerpt というコマンドがあります。
各エントリーに抜粋が入力されていればその抜粋文を、なければ本文から110文字を取得するコマンドです。
※110文字は WP Multibyte Patch によって実現しています。詳細はgoogle先生に聞いてください。
indexなどで記事の抜粋を表示するときに使うのですが、これを自分なりに作成してみたのでそのまま記載。
もっとキレイなコードを書けるようになりたい。。。
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表記・余分な改行と空白
Source
function tkb_get_excerpt_ja($cnt_textlen = 100, $my_content = "") { if ($my_content==""){ $my_content = get_the_content('[more]'); } $cnt_sentence = ceil($cnt_textlen/30); //読みやすい1sentenceの平均文字数は33〜35文字らしい $ptn_sentenceend = "(。|?|!|♪|)n|)n)"; //文末判定文字-正規表現 $my_content = preg_replace("/[rn|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 (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)."… [more]"; } else { $ret_content = $my_preg[0]." [more]"; } } else { $ret_content = mb_substr($my_content, 0, $cnt_textlen)."… [more]"; } } else { $ret_content = $my_content; } $ret_content = preg_replace("/s+/m", " ", $ret_content); //連続するスペース改行を1つに return $ret_content; }
Notes
んー、臆面もなくこんなソースを晒していいのか。以下、考え中のこと。
<!–more–>タグをまったく意識していない。<!–more–>タグ以降は抜粋しないほうがいい?- 文末判定文字をもう少し深く考えたい。英文だとお手上げ。ちなみに「)+改行」は顔文字用で(爆
- [more] にリンク貼る?リンクありなしオプション、文字列カスタマイズわかりやすく先頭になど
- つかいやすいようにspanとかclass指定とか
- 各エントリーに抜粋データがあっても完全無視、私がモノグサで入力しない前提
※2013.1.20追記
<!–more–>タグに関しては読み込まれるページによって変わります。
single/pageなどの単体ページから呼び出すと全文から抽出します。
それ以外のindexやsearchページでは、get_the_content は<!–more–>以降を取得しません。
global $more; $more = 1; をループの前に書くと全文表示できる?
この[more]がちょうど区切りにきた場合、ヘンな出力される可能性がある、、。
このような関数は functions.php に書くのですが途中でトラブル発生したのでメモ。
ググればいっぱいでてきますが、functions.php にはお作法があります。失敗すると真っ白けです。
- <?php 〜 ?>の外に空白、改行はNG → 先頭行に<?php、?>は省略可能なので記載しない。
- 文字コードは UTF-8 BOMなし(UTF-8N) → 今回これでトラブル発生、いつのまにか(´;ω;`)
functions.php はこまめにバックアップをとっておきましょう。
エラーで真っ白になったらftpで引き取って「UTF-8 BOMなし」で保存、上書きアップロードで解決できる、、ハズ。
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件のフィードバック
コメントは停止中です。