category.php
投稿記事に付けられる「カテゴリー」属性によって、分類表示する時に利用されるテンプレートファイルです。通常は「カテゴリー」に属する投稿記事が日付順に表示されます。
ブログでは「カテゴリー」属性別の表示は多用されていて、とても一般的です。テーマの中に「category.php」がない場合はテーマ内の「archive.php」が、「archive.php」がない場合は「index.php」が代行します。(テンプレートファイルの階層)
「カテゴリー」別にテンプレートを分けたい場合はファイル名をID別の表記(category-xxx.php)にすることで、分類することができます。
*「Default_Theme(英語版)」の「アーカイブテンプレート(archive.php)」を修正して、「category.php」を制作してみましょう。
category.php 例
<?phpget_header();?> //インクルードファイル:header.phpを読み込みます
<div id="content" class="narrowcolumn">
<?php if (have_posts()) : ?> //属性別の条件式の開始
<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
//カテゴリータイトル表示
<h2 class="pagetitle">Archive for the ‘<?php single_cat_title(); ?>’ Category</h2>
//投稿前後のナビゲーションの表示
<div class="navigation">
<div class="alignleft"><?php next_posts_link('? Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries ?') ?></div>
</div>
<?php while (have_posts()) : the_post(); ?> //コンテンツデータを取得する・ループのPHPコードの開始
<div <?php post_class() ?>>
//投稿(コンテンツ)タイトル名の表示
<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<small><?php the_time('l, F jS, Y') ?></small>
//投稿(コンテンツ)内容の表示
<div class="entry">
<?php the_content() ?>
</div>
//投稿タグ・カテゴリー・編集リンク・コメントの表示
<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments ?', '1 Comment ?', '% Comments ?'); ?></p>
</div>
<?php endwhile; else: ?> //コンテンツデータを取得する・ループのPHPコードの終了
//コンテンツデータがないときのメッセージ表示
<h2 class='center'>Sorry, but there aren't any posts in the %s category yet.</h2>
<? php endif; ?> //属性別の条件式の終了
//投稿前後のナビゲーションの表示
<div class="navigation">
<div class="alignleft"><?php next_posts_link('? Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries ?') ?></div>
</div>
</div>
<?php get_sidebar(); ?> //インクルードファイル:sideber.phpを読み込みます
<?php get_footer(); ?> //インクルードファイル:footer.phpを読み込みます
関連情報:
- Default/archive.php
- Default/index.php
- Default/single.php
- Default/page.php
- テンプレートファイルの階層
- Default/comments.php
- Default/sidebar.php
2009.05.03 17;23



コメント&トラックバック
トラックバックURL: http://www.exe1993.jp/wpbusiness/archives/category_php/trackback/