先日の記事で固定ページにphpファイルを挿入出来るようにしたので、phpを利用して固定ページにサムネイル付の最新投稿記事を表示出来るようにしていきます。
ちなみに先日の記事は↓です。
【wordpress】投稿・固定ページ内にPHPファイルを挿入する
function.phpに以下を追記します。
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 200, 200, false ); // サムネイルのサイズ
}
size( 200, 200, false )の200はサムネイル画像のサイズ、またfalseはリサイズ、trueにすれば切り抜きになります。
続いて固定ページに挿入するphpを作成します。
<?php
query_posts('posts_per_page=5');
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<dl>
<dt class="text"><a href="<?php the_permaLink(); ?>"><?php
if ( has_post_thumbnail() ): // サムネイルを持っているときの処理
?><?php the_post_thumbnail( array(200,200), array('class' => 'imgLeft') ); ?><?php
else: // サムネイルを持っていないときの処理
?>img src="<?php bloginfo('template_url') ?>/image/no_image.gif" alt="noimage" class="imgLeft" /><?php
endif; ?><?php the_title(); ?></a></dt>
<dd class="kiji"><?php the_excerpt(); ?></dd>
<dd class="date"><?php the_time('Y.m.d'); ?>UP</dd>
</dl>
<?php
endwhile;
endif;
wp_reset_query();
?>
posts_per_page=5の数字を変更すると表示する最新記事の数を変更できます。
また、部分の設定を変更すればカテゴリーを限定したりも出来るみたいです。
cssでデザインなどを変更してサイトに合わせてください。
参考サイト
wordpress.org様
有難うございました。


