2014年08月25日 web関連WordPressphp

WordPressの抜粋表示(excerpt)を以下のように動作させたかったので、自作してみました。

  1. 抜粋があれば抜粋表示
  2. moreタグがあれば、それに従う
  3. 指定文字数より本文文字数が少なければ、そのまま表示
  4. 指定文字数より本文文字数が多ければ抜粋表示

コード

functions.phpにコピペ。

<?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;
}

?>

使用方法

テーマファイルの本文の抜粋を表示させたいにset_excerpt();

この投稿へのコメント

コメントはまだありません。

コメントを残す

メールアドレスが公開されることはありません。

次のHTML タグと属性が使えます。
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CAPTCHA


ピックアップ記事

管理画面上の投稿フォームにHTMLタグ挿入ボタンを追加

2011年11月21日 WordPress
AddQuicktag 管理画面上の投稿フォームにHTMLタグ挿入ボタンを自由に追加出来る green coffee bean extract main…
「管理画面上の投稿フォームにHTMLタグ挿入ボタンを追加」をはてなブックマークに追加

ページIDからリンクを設定

2011年11月28日 WordPress
[php] <a href="<?php echo get_permalink('75'); ?>">お問い合わせ</a> [/php]
「ページIDからリンクを設定」をはてなブックマークに追加

WordPressで特定の親カテゴリとその子カテゴリに所属する場合の条件分岐の方法

2022年11月12日 WordPress
functions.phpに以下を記述 [php]if ( ! function_exists( 'post_is_in_descendant_category' ) ) { function post_is_in_descendant_category( $cats, $_post = null ) { foreach ( (array) $cats as $c…
「WordPressで特定の親カテゴリとその子カテゴリに所属する場合の条件分岐の方法」をはてなブックマークに追加

FireFoxのメニューバーを表示させる

2011年06月04日 web関連
Windowsで、FireFoxのメニューバーを非表示にしてしまうと、再表示させる方法が分かりづらいのでメモ。 解決方法 操作は簡単で、FireFoxを表示中に、「ALT」を押す! 以上です (´ー`) Macの場合は各アプリケーションのメニューは画面の一番上に表示されているので、メニューバーの表示、非表示はいつでも簡単に切り替えられます。…
「FireFoxのメニューバーを表示させる」をはてなブックマークに追加

WordPressでindex.htmlとindex.phpを混在させる

2011年06月14日 WordPress
なんともない話題ではありますが、既存のサイトを公開したまま、index.phpの方で、WordPressを入れて、リニューアルをと思ってたんですが、index.phpもindex.htmlもWordPressのホームディレクトリにリダイレクトされてるようです。 どうしたもんかと思ってたら、簡単に解決する方法がありました。 wp-include内のcanonical.phpファイルを開き、270行…
「WordPressでindex.htmlとindex.phpを混在させる」をはてなブックマークに追加
© graffiti on the web . All rights reserved. WordPress Theme by comfy