PR

[WordPress] カテゴリページでカテゴリ内の記事を一覧表示

WordPress でカテゴリページにアクセスした際に、カテゴリ内の記事を一覧表示する方法。

通常は、トップページなどと同様に記事のタイトルや投稿日時、ボティーが表示されるが、ここではタイトルのみの一覧を表示する。

要はカテゴリページを開いたら、そのカテゴリ以下の全記事を一覧表示したい。

※例としてvicuna.exc テーマの場合

変更前

category.php の該当部分は次のようになっている。


<?php
 if (have_posts()) :
 while (have_posts()) : the_post();
?>

 <div id="entry<?php the_ID(); ?>">
 <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
 <ul>
 <li><?php the_time(__('Y-m-d (D)', 'vicuna')) ?></li>
 <li><?php the_category(' | ') ?></li>
 <?php /* -- ex cng s -- */
 vicuna_author_displays();
 vicuna_tag_displays(); ?>
 <?php //if (function_exists('the_tags')) : the_tags('<li>', ' | ', '</li>'); endif; ?>
 <?php /* -- ex cng e -- */ ?>
 <?php edit_post_link(__('Edit'), '<li>', '</li>'); ?>
 </ul>
 <div>
<?php the_content(__('Continue reading', 'vicuna')); ?>
 </div>
 <ul>
<?php    $trackpingCount = get_vicuna_pings_count();
/* -- whisper cng s --
 $commentCount = (int) get_comments_number() - (int) $trackpingCount;
-- */
 $commentCount = (int) get_vicuna_comment_count() - (int) $trackpingCount;
/* -- whisper cng e -- */
 if ($commentCount > 0 || 'open' == $post->comment_status) : ?>
 <li><a href="<?php the_permalink() ?>#comments" title="<?php printf(__('Comments on %s'), $title); ?>" rel="nofollow"><?php if ('open' == $post->comment_status) : _e('Comments'); else : _e('Comments (Close)', 'vicuna'); endif; ?></a>: <span><?php echo $commentCount; ?></span></li>
<?php            else : ?>
 <li><?php _e('Comments (Close)', 'vicuna'); ?>: <span><?php echo $commentCount; ?></span></li>
<?php
 endif;
 if ($trackpingCount > 0 || 'open' == $post->ping_status) :
?>
 <li><a href="<?php the_permalink() ?>#trackback" title="<?php printf(__('Trackbacks to %s'), $title); ?>" rel="nofollow"><?php if ('open' == $post->ping_status) : _e('Trackbacks'); else : _e('Trackbacks (Close)', 'vicuna'); endif; ?></a>: <span><?php echo $trackpingCount; ?></span></li>
<?php            else : ?>
 <li><?php _e('Trackbacks (Close)', 'vicuna'); ?>: <span><?php echo $trackpingCount; ?></span></li>
<?php            endif ?>
 </ul>
 </div>
<?php
 endwhile;
 endif;
?>

スポンサードリンク

変更後

以下のように変更する。


<?php query_posts($query_string . "&order=ASC&orderby=title&showposts=-1"); ?>

<?php
 if (have_posts()) :
 while (have_posts()) : the_post();
?>

 <div id="entry<?php the_ID(); ?>">
 <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
 </div>
<?php
 endwhile;
 endif;
?>


# この方法だとSQL のlimit に当たる物を取り除いているのど同義だから、あまりにも記事が増えた場合はカテゴリ分けを見なおさないと、負荷がかかるかもしれない

参考文献

  • WordPressの基本操作と使い方|WordPressのツボ

コメント

  1. NA より:

    半日悩んでいた一覧表示がここで1分で解決しました!
    助かりました。すごく感謝です!!

    他にも勉強になりそうな記事がたくさんありそうなので、またお勉強させてください~☆