Tag: excerpt


文字数をコントロール|WordPress

2013年12月11日 wordpressテンプレートタグ

抜粋の文字

記事本文内から抜粋した内容を表示

<?php the_excerpt(); ?>

これは、110文字表示されて、最後に[…]が表示されます。

抜粋の文字数をコントロール

<?php echo mb_substr(get_the_excerpt(), 0, 30); ?>

この場合、110文字以下の数字であれば、これでOKです。

 

抜粋の文字数の変更

functions.phpに以下を記述

//概要(抜粋)の文字数調整
function my_excerpt_length($length) {
	return 200;
}
add_filter('excerpt_length', 'my_excerpt_length');

「return 200;」は200文字ということです。

文末の[…]を削除する方法

functions.phpに以下を記述

//概要(抜粋)の省略文字
function my_excerpt_more($more) {
	return '';
}
add_filter('excerpt_more', 'my_excerpt_more');

文末の[…]を別の文字に変更する方法

functions.phpに以下を記述

//概要(抜粋)の省略文字
function my_excerpt_more($more) {
	return 'ここを変更!!';
}
add_filter('excerpt_more', 'my_excerpt_more');

 

タイトルの文字

40文字に制限したいとき
mb_substr(文字列,何文字目から取り出すか,何文字取り出すか)

 

<?php $title= mb_substr($post->post_title,0,40); echo $title; ?>

 

 

指定した文字数で省略後に「・・・」、指定した文字数以内であればそのまま表示

 <?php if(mb_strlen($post->post_title)>23) { $title= mb_substr($post->post_title,0,23) ; echo $title. ・・・ ;} else {echo $post->post_title;}?>