Last active
June 16, 2018 09:53
-
-
Save sachixx/3449e57f1b96056e2845ff364eeb44de to your computer and use it in GitHub Desktop.
本文抜粋を取得する関数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//function.phpに記述 | |
function get_the_custom_excerpt($content, $length) { | |
$length = ($length ? $length : 70);//デフォルトの長さを指定する | |
$content = preg_replace('/<!--more-->.+/is',"",$content); //moreタグ以降削除 | |
$content = strip_shortcodes($content);//ショートコード削除 | |
$content = strip_tags($content);//タグの除去 | |
$content = str_replace(" ","",$content);//特殊文字の削除(今回はスペースのみ) | |
$content = mb_substr($content,0,$length);//文字列を指定した長さで切り取る | |
return $content; | |
} | |
//表示させたいページのテンプレートに記述 | |
<?php echo get_the_custom_excerpt( get_the_content(), 50 )."…続きを読む"; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment