Last active
August 29, 2015 14:05
-
-
Save apr20/8821b8c6b3f3614d97b2 to your computer and use it in GitHub Desktop.
WordPressの抜粋表示設定
This file contains 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 | |
//WordPress抜粋表示の変更 | |
/**** | |
1.抜粋があれば抜粋表示 | |
2.moreタグがあれば、それに従う | |
3.$lengthより文字が少なければ、そのまま表示 | |
4.$lengthより文字が多ければ抜粋表示 | |
****/ | |
function set_excerpt( $length =200){ | |
global $post; | |
//抜粋が入力されていれば、抜粋表示($length無効) | |
if(has_excerpt()) { | |
$content = strip_tags(get_the_excerpt()); | |
$content = $content . '…'.'<div class="more"><a href="'.get_permalink().'" class="btn btn-default btn-md"><span class="glyphicon glyphicon-ok"></span> この記事の続きを読む</a></div>'; | |
//moreタグがある場合 | |
//}elseif(preg_match('/<!--more(.*?)?-->/u', $post->post_content, $matches)){ | |
}elseif(strpos($post->post_content,'<!--more')){ | |
global $more; $more = 0; | |
$content = strip_tags(get_the_content('')); | |
$content = $content . '…'.'<div class="more"><a href="'.get_permalink().'" class="btn btn-default btn-md"><span class="glyphicon glyphicon-ok"></span> この記事の続きを読む</a></div>'; | |
//本文の文字数が$lengthより少ない場合は全文表示 | |
}elseif(mb_strlen( $post -> post_content) <= $length){ | |
$content = strip_tags(get_the_content()); | |
}else{ | |
$content = mb_substr( strip_tags( $post -> post_content ), 0, $length ); | |
$content = $content . '…'.'<div class="more"><a href="'.get_permalink().'" class="btn btn-default btn-md"><span class="glyphicon glyphicon-ok"></span> この記事の続きを読む</a></div>'; | |
} | |
return $content; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment