これまでのやりとりから、私が解釈した形式でのコードを書いてみました。もう少しきれいに書けると良かったのですが悪しからず。 WP2.8.4 、 Default テーマでの動作を確認しています。
WordPress ループの部分は適宜変更してください。ただ、ページ送り部分のテンプレート関数の位置には注意してください。場所によっては思わぬ挙動をするはずです。
<?php
if ( is_category() ) :
// クエリからカテゴリーとページ数を取得
$catid = get_query_var( 'cat' );
$paged = get_query_var( 'paged' );
// 子カテゴリーを配列で取得
$children = get_term_children( $catid, 'category' );
// 子カテゴリー一覧の表示件数 (sp = showposts)
$child_sp = 5;
// クエリを発行してしまうと、 single_cat_title() が機能しなくなる
$single_cat_title = single_cat_title( '', false );
// Default テーマの場合次の行のコメントを解除すると幸せ
// $single_cat_title = printf(__('Archive for the ‘%s’ Category', 'kubrick'), $single_cat_title);
// 子カテゴリーが存在する場合、それらを除くクエリを発行
if ( !empty( $children ) ) {
query_posts( array(
'category__in' => array( $catid ),
'category__not_in' => $children,
'paged' => $paged,
) );
}
?>
<?php if ( have_posts() ) : ?>
<h2 class="pagetitle"><?php echo $single_cat_title; ?></h2>
<?php posts_nav_link(); /* WP_PageNavi 等と置き換え */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div <?php post_class(); ?>>
<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<small><?php the_time(__('l, F jS, Y')) ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
<p class="postmetadata"><?php the_tags(__('Tags:'), ', ', '<br />'); ?> <?php printf(__('Posted in %s'), get_the_category_list(', ')); ?> | <?php edit_post_link(__('Edit'), '', ' | '); ?> <?php comments_popup_link(__('No Comments »'), __('1 Comment »'), __('% Comments »'), '', __('Comments Closed') ); ?></p>
</div>
<?php endwhile; ?>
<?php posts_nav_link(); /* WP_PageNavi 等と置き換え */ ?>
<?php endif; ?>
<?php
// 子カテゴリーが存在する場合
if ( !empty( $children ) ) :
foreach ( $children as $child ) :
// 子カテゴリーのオブジェクトを取得
$child_cat_object = get_category( $child );
// 孫カテゴリーはスキップ
if ( $child_cat_object->category_parent != $catid ) continue;
// 子カテゴリー用のクエリを作成
query_posts( array(
'cat' => $child,
/*
* 孫カテゴリーを子カテゴリーの記事として含めない場合は 'cat' => $child ではなく
* 'category__in' => array( $child ),
*/
'showposts' => $child_sp,
) );
?>
<?php if ( have_posts() ) :
// 子カテゴリーページのタイトルと URL を取得
$child_cat_title = apply_filters( 'single_cat_title', get_cat_name( $child ) );
$child_cat_link = get_category_link( $child );
?>
<h2 class="pagetitle"><a href="<?php echo $child_cat_link; ?>"><?php echo $child_cat_title; ?></a> の最新記事</h2>
<?php while ( have_posts() ) : the_post(); ?>
<div <?php post_class(); ?>>
<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<small><?php the_time(__('l, F jS, Y')) ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
<p class="postmetadata"><?php the_tags(__('Tags:'), ', ', '<br />'); ?> <?php printf(__('Posted in %s'), get_the_category_list(', ')); ?> | <?php edit_post_link(__('Edit'), '', ' | '); ?> <?php comments_popup_link(__('No Comments »'), __('1 Comment »'), __('% Comments »'), '', __('Comments Closed') ); ?></p>
</div>
<?php endwhile; ?>
<p class="child-nav"><a href="<?php echo $child_cat_link; ?>">「<?php echo $child_cat_title; ?>」カテゴリーの記事一覧へ »</a></p>
<?php endif; ?>
<?php
endforeach;
// query_posts を発行している場合にクエリをリセット
wp_reset_query();
endif;
endif;
?>
安直に投稿してみましたが、これで正解なのか実際にはイマイチ自信がありません。動作を確認して頂いて、そうじゃないよ、というところがあれば遠慮なくご指摘ください。修正します。
# 一部フォーラムの仕様で、数値文字参照と実体参照がうまくエスケープできてません。コピーしたあとに修正が必要です。