カテゴリーアーカイブのページテンプレート。
各カテゴリーの新着記事を表示したアーカイブページが作れます
参考WordPress How To: Latest Posts by Category Archive
テーマディレクトリに category-archive.php を、作成して次のコードを記述しアップロード。
※使用中のテーマに合わせて変更して下さい。
<?php
/*
Template Name: Category Archive
*/
?>
<?php get_header(); ?>
<div id="content" class="narrowcolumn">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>
<?php wp_link_pages(array('before' => '<p>Pages: ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
</div>
<?php endwhile; endif; ?>
<ul class="catArchive">
<?php
$catQuery = $wpdb->get_results("SELECT * FROM $wpdb->terms AS wterms INNER JOIN $wpdb->term_taxonomy AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category' AND wtaxonomy.parent = 0 AND wtaxonomy.count > 0");
$catCounter = 0;
foreach ($catQuery as $category) {
$catCounter++;
$catStyle = '';
if (is_int($catCounter / 2)) $catStyle = ' class="catAlt"';
$catLink = get_category_link($category->term_id);
echo '<li'.$catStyle.'><h3><あ href="'.$catLink.'" title="'.$category->name.'">'.$category->name.'</あ></h3>';
echo '<ul>';
query_posts('cat='.$category->term_id.'&showposts=5');?>
<?php while (have_posts()) : the_post(); ?>
<li><あ href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></あ></li>
<?php endwhile; ?>
<li><あ href="<?php echo $catLink; ?>" title="<?php echo $category->name; ?>">More <?php echo $category->name; ?></あ></li>
</ul>
</li>
<?php } ?>
</ul>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
表示させたくないカテゴリーがある場合は次の部分に次のコードを加える
AND wterms.term_id NOT IN (2,5,6)");
こんな感じ
修正前
$catQuery = $wpdb->get_results("SELECT * FROM $wpdb->terms AS wterms INNER JOIN $wpdb->term_taxonomy AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category' AND wtaxonomy.parent = 0 AND wtaxonomy.count > 0");
修正後
$catQuery = $wpdb->get_results("SELECT * FROM $wpdb->terms AS wterms INNER JOIN $wpdb->term_taxonomy AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category' AND wtaxonomy.parent = 0 AND wtaxonomy.count > 0 AND wterms.term_id NOT IN (2,5,6)");
カテゴリー2、5、6が除外される。
逆に、指定したカテゴリーだけ表示する場合は次の場所を変更する
wterms.term_id NOT IN (2,5,6)");
wterms.term_id IN (2,5,6)");
カテゴリー2,5,6だけ表示される。
記事リストの表示数の変更は showposts=5 の箇所を変更。
デフォルトは5
次にスタイルシートに次を追加。
※使用中のテーマにあわせて変更
.catArchive {
width: 450px;
overflow: hidden;
margin: 20px 0 0 0;
padding: 0;
list-style-type: none;
}
.catArchive h3 {
font: normal bold 18px sans-serif;
border-bottom: 1px solid #666;
margin: 0;
padding: 0 0 3px 0;
display: block;
}
.catArchive li {
display: block;
float: left;
width: 210px;
margin: 0 30px 30px 0;
}
.catArchive ul {
margin: 0;
padding: 0;
}
.catArchive li li {
border-bottom: 1px solid #ddd;
margin: 0;
padding: 4px 0;
}
.catAlt {
margin-right: 0 !important;
}
管理画面から、カテゴリーアーカイブ用の新規ページを作成し、その際に Category Archive の、テンプレートを使用する。