固定ページへの機能の追加と、表示

固定ページには、標準の状態では、抜粋欄がありません。

WordPress3.0から、add_post_type_support 投稿タイプ( 投稿タイプは標準で、post,page,attachment,revision,nav_menu_itemがあります。)に、機能を追加できるようになりました。

投稿タイプ、pageに、抜粋を追加し、固定ページページ一覧を表示させてみましょう

テーマファイルのfunctions.php に 以下の一行を設定する事で、pageに、抜粋(excerpt)機能が登録されます。

add_post_type_support( 'page', 'excerpt' );

以下のテンプレートを作成し、add-excerpt.phpとします。

固定ページを作成し、テンプレート Add excerpt for page を選択して保存します。

このテンプレートは、公開状態のページを6件、抜粋付で表示します。

<?php
/**
 * Template Name: Add excerpt for page
 *
 *
 *
 */
get_header( );?>
<div>
<?php
global $post;
$numberposts= '6'; //一覧表示したい記事数
$postslist= get_posts( "post_status=publish&amp;post_type=page&amp;numberposts={$numberposts}&amp;order=DESC&amp;orderby=date" );
?>
<ul class="update">
<?php
foreach ( $postslist as $post ) {
	setup_postdata( $post );
?>
	<li><h2><a href="<?php the_permalink(); ?>"><?php echo the_title(); ?></a></h2></li>
	<li><?php the_time('m月d日'); ?></li>
	<li><p>
<?php 	the_excerpt();?></p></li>
<?php }
	wp_reset_postdata( );
?>
</ul>
</div>
<?php get_footer( ); ?>

ページに追加可能な、その他の機能

  • アイキャッチ画像(thumbnail)
  • トラックバック送信(trackbacks)
  • フォーマット(post-formats)

を必要により追加する事が出来ます。

ページに追加した抜粋をmeta descriptionとして利用する

functions.php にコードを追加

<?php
//ページのdescription用に抜粋を追加
add_post_type_support( 'page', 'excerpt' );
//meta要素を<head>にセット
add_action( 'wp_head', 'my_description' );
//ページのdescription用に抜粋フィールド名の変更
add_filter('admin_head','change_category_title' );
 
function my_description(){
 global $post;
 if( is_page() ){
		$post_data = get_post( $post->ID );
		$html 	= '<meta name="description" content="%1$s" />'."\n";
		printf( $html, esc_attr( $post_data->post_excerpt ) );
 }
}

function change_category_title(){
	$script=<<<SCRIPT
	<script type="text/javascript">
	 jQuery(function() {
		jQuery("#postexcerpt h3 span").html('Meta Description');
	 });
	</script>
SCRIPT;
	 
	$condition= $_SERVER['QUERY_STRING'];
	
	if( preg_match( '!post_type=page!',$condition ) ){
		echo $script;
	}

}
?>

[emulsion_relate_posts]