« | »

Default/archive.php

「アーカイブテンプレート(archive.php)」は「カテゴリー」「日付」「ユーザ」「タグ」などの投稿記事に付けられる属性によって、分類表示しようとする時に利用されるテンプレートファイルです。テンプレートファイルの階層としては「記事用テンプレート(single.php)」と同じく、最も汎用性のある階層の「メインテンプレート(index.php)」に次いで、汎用性の高いファイルです。

「アーカイブテンプレート(archive.php)」では投稿記事に付いている属性(「カテゴリー」「日付」「ユーザ」「タグ」など)別の表示を条件式で、全て表現することもできますが、「カテゴリー別」や「日付別」、「ユーザー別」、「タグ別」などの個別テンプレートを作成して、属性別のページを作成することできます。

ブログなどの構造が比較的に単純なものは「アーカイブテンプレート(archive.php)」のみで運用するほうが分かりやすく良いかと思いますが、構造が複雑であろうWebサイトでの利用の際は、個別テンプレートを作成して、構築する方が、分かりやすいテーマになるでしょう。

「アーカイブテンプレート(archive.php)」から派生できる主なテンプレートは次のとおりです。

  • 1.category.php :カテゴリ用テンプレート/カテゴリー別のコンテンツ一覧表示
  • 2.author.php :著者用テンプレート/ユーザ別のユーザ・筆者紹介
  • 3.date.php :日時用テンプレート/年月別のコンテンツ一覧表示

*「Default_Theme(英語版)」の「アーカイブテンプレート(archive.php)」をコメント文(//説明文)を使って機能解説してみましょう。

Default_Theme/archive.php


<?php
/**
 * @package WordPress
 * @subpackage Default_Theme
 */
get_header();?>          //インクルードファイル:header.phpを読み込みます

	<div id="content" class="narrowcolumn">

		<?php if (have_posts()) : ?>     //属性別の条件式の開始

 	  <?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>

      //「カテゴリー」アーカイブの際のタイトル・コメント表示(カテゴリー別一覧表示に使用)
 	  <?php /* If this is a category archive */ if (is_category()) { ?>
		<h2 class="pagetitle">Archive for the ‘<?php single_cat_title(); ?>’ Category</h2>

      //「タグ」アーカイブの際のタイトル・コメント表示(タグ別一覧表示に使用)
 	  <?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
		<h2 class="pagetitle">Posts Tagged ‘<?php single_tag_title(); ?>’</h2>

      //「日別」アーカイブの際のタイトル・コメント表示(xx年xx月xx日別一覧表示に使用)
 	  <?php /* If this is a daily archive */ } elseif (is_day()) { ?>
		<h2 class="pagetitle">Archive for <?php the_time('F jS, Y'); ?></h2>

      //「月別」アーカイブの際のタイトル・コメント表示(xx年xx月別一覧表示に使用)
 	  <?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
		<h2 class="pagetitle">Archive for <?php the_time('F, Y'); ?></h2>

      //「年別」アーカイブの際のタイトル・コメント表示(xx年別一覧表示に使用)
 	  <?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
		<h2 class="pagetitle">Archive for <?php the_time('Y'); ?></h2>

      //「投稿者(著者)別」アーカイブの際のタイトル・コメント表示(投稿者(著者)別一覧表示に使用)
	  <?php /* If this is an author archive */ } elseif (is_author()) { ?>
		<h2 class="pagetitle">Author Archive</h2>

      //属性分類がない場合のタイトル表示
 	  <?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) <amp;<amp; !empty($_GET['paged'])) { ?>
		<h2 class="pagetitle">Blog Archives</h2>
 	  <?php } ?>

      //投稿前後のナビゲーションの表示
		<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; ?>          //コンテンツデータを取得する・ループのPHPコードの終了

        //投稿前後のナビゲーションの表示
		<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 else :

		if ( is_category() ) { // If this is a category archive
			printf("<h2 class='center'>Sorry, but there aren't any posts in the %s category yet.</h2>", single_cat_title('',false));
		} else if ( is_date() ) { // If this is a date archive
			echo("<h2>Sorry, but there aren't any posts with this date.</h2>");
		} else if ( is_author() ) { // If this is a category archive
			$userdata = get_userdatabylogin(get_query_var('author_name'));
			printf("<h2 class='center'>Sorry, but there aren't any posts by %s yet.</h2>", $userdata->display_name);
		} else {
			echo("<h2 class='center'>No posts found.</h2>");
		}
		get_search_form();

	endif;
?>  //属性別の条件式の終了

	</div>

<?php get_sidebar(); ?>          //インクルードファイル:sideber.phpを読み込みます

<?php get_footer(); ?>          //インクルードファイル:footer.phpを読み込みます
  • 1.ヘッダー(header)部分、サイドバー(sidebar)部分、フッター(footer)部分は共通部品として、それぞれのインクルードファイルが読み込まれています。
  • 2.コンテンツデータを取得するループのPHPコードによって、DBからコンテンツデータを取得して、タイトルと内容を表示しています。
  • 3.コンテンツデータに関する投稿日付、投稿者、タグ、カテゴリー等を表示しています。
  • 4.仮にコンテンツデータがないときにはメッセージを表示します。

関連情報:

  1. category.php

  2. Default/sidebar.php

  3. テンプレートファイルの階層

  4. Default/single.php

  5. Default/index.php

  6. Default/footer.php

  7. テンプレートファイル

2009.04.14 03;18

« | »

⇒この記事を印刷する

コメント&トラックバック

トラックバックURL: http://www.exe1993.jp/wpbusiness/archives/default_archive_php/trackback/





このページのトップへ

ewb_blue-l_160170.png
art-img.gif