サポート » 使い方全般 » 子カテゴリーの記事タイトル一覧の表示順を変更したい

  • 解決済 ryosuke

    (@ryosuke)


    先日、
    [解決済み] 親カテゴリーページで子カテゴリーの記事タイトル一覧を表示したい
    でご教示いただきました方法ですが、
    子カテゴリーの表示順を変更することは可能でしょうか。

    現在、カテゴリー名順に表示されておりますが、
    これをスラッグ順に変更したい目的です。

    何度も申し訳ございませんが、何卒ご教示いただけますと助かります。

    <?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 &raquo;'), __('1 Comment  &raquo;'), __('% Comments  &raquo;'), '', __('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 &raquo;'), __('1 Comment  &raquo;'), __('% Comments  &raquo;'), '', __('Comments Closed') ); ?></p>
    
    		</div>
    
    		<?php endwhile; ?>
    
    		<p class="child-nav"><a href="<?php echo $child_cat_link; ?>">「<?php echo $child_cat_title; ?>」カテゴリーの記事一覧へ &raquo;</a></p>
    
    	<?php endif; ?>
    
    <?php
    		endforeach;
    		// query_posts を発行している場合にクエリをリセット
    		wp_reset_query();
    	endif;
    endif;
    ?>
2件の返信を表示中 - 1 - 2件目 (全2件中)
  • こんばんは。スラッグ順にソートしたい場合は、 get_term_children() の代わりに get_terms() を使って子カテゴリーを取得します。

    // 子カテゴリーを配列で取得
    $children = get_term_children( $catid, 'category' );

    の部分を

    // 子カテゴリーを配列で取得 (スラッグ順)
    $children = get_terms( 'category', array( 'child_of' => $catid, 'orderby' => 'slug', 'fields' => 'ids' ) );

    のように変更してみてください。今更ですが get_terms() を使っておいたほうが、より柔軟に条件を指定できますし、コードの方ももうちょっとシンプルに出来たかもしれません。

    トピック投稿者 ryosuke

    (@ryosuke)

    mizubeさま

    さっそくのご回答有り難うございます!

    無事反映させることができました。

    何度もお尋ねしてしまい申し訳ございません。
    おかげさまで本当に助かりました。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「子カテゴリーの記事タイトル一覧の表示順を変更したい」には新たに返信することはできません。