記事の抜粋を表示するために「the_excerpt();」を使います。
前の記事でpage-home.phpに追加したコードを少し改良して、カテゴリー事に表示される記事タイトルを、それぞれ最初の投稿だけタイトルと記事抜粋を表示するようにします。
【変更、追加コード:page-home.php】
<?php
$categories = get_categories(array(
'parent'=>0,
'pad_counts'=>1));
foreach($categories as $category) :
$count = true;
?>
<h2 class="entry-title"><?php echo $category->cat_name; ?></h2>
<ul>
<?php
query_posts('showposts=5&cat='.$category->cat_ID);
if (have_posts()) : while (have_posts()) : the_post();
?>
<?php if($count): ?>
<li><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a><br /><?php the_excerpt(); ?></li>
<?php $count=false; ?>
<?php else: ?>
<li><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endif; ?>
<?php endwhile; endif; ?>
</ul>
<?php wp_reset_query(); ?>
<?php endforeach; ?>
スポンサーリンク


コメント