yuheijotaki.com

2016/10/19 : 
WordPress でよく使うコードを functions.php にまとめる

タイトルの出力
◯functions.php

function output_title() {
	global $post;
	echo $post->post_title;
}

 
◯テンプレートファイル側

<?php output_title(); ?>

 
 

タイトルの出力 〜文字
◯functions.php

function output_title_ellipsis($length=20) {
	global $post;
	if ( $length && mb_strlen($post->post_title, 'utf-8') > $length ) {
		$title= mb_substr( $post->post_title,0, $length, 'utf-8' );
		echo $title. '...' ;
	} else {
		echo $post->post_title;
	}
}

 
◯テンプレートファイル側

<?php output_title_ellipsis(10); ?>