WordPress 本文抜粋110文字まで表示「the_excerpt()」

Filed under: functions.php,wordpress — kdcs @ 17年12月8日 金曜日

WordPressで本文抜粋110文字まで表示できるタグ

<?php the_excerpt(); ?>

基本は英数字55単語となっているようで、日本語110文字に対応させるには「WP Multibyte Patch」プラグインを有効にする必要があります。

このテンプレートタグを使用した場合、記事本文の110文字の後に[…]が表示されます。
このタグを使うとpタグで囲まれるのでpタグを外すには以下のコードを使う

<?php echo get_the_excerpt(); ?>

◆表示文字数を変える(function.phpに以下を記述)

//the_excerpt() 抜粋文字数の変更 ---------------
function my_excerpt_length($length) {
	return 80;
}
add_filter('excerpt_length', 'my_excerpt_length');

◆文末の[…]を削除する方法(function.phpに以下を記述)

//the_excerpt() 抜粋の省略文字削除 -------------
function my_excerpt_more($more) {
	return '';
}
add_filter('excerpt_more', 'my_excerpt_more');

◆文末の[…]を別の文字に変更する(function.phpに以下を記述)

//the_excerpt() 抜粋の省略文字を変更 -----------
function my_excerpt_more($more) {
	return '…';
}
add_filter('excerpt_more', 'my_excerpt_more');

◆別の文字に変更して、その記事へのリンクを追加する(function.phpに以下を記述)

function my_excerpt_more($post) {
	return '<a href="'. get_permalink($post->ID) . '">' . '…続きを読む' . '</a>';
}
add_filter('excerpt_more', 'my_excerpt_more');

サイト内検索

カテゴリー

最近の投稿

« |WordPress 本文抜粋110文字まで表示「the_excerpt()」| »
↑上に戻る