« | »

Default/single.php

「記事用テンプレート(single.php)」は投稿記事のみの表示となります。テンプレートファイルの階層としては最も汎用性のある階層の「メインテンプレート(index.php)」に次いで、汎用性の高いファイルです。

「イメージテンプレート(image.php)」や「ビデオテンプレート(video.php)」などのテンプレートファイルがテーマ内にない場合、「記事用テンプレート(single.php)」が代行して、その役割を果たします。

基本的には「メインテンプレート(index.php)」の構成に似ていますが、記事に対する「コメント」欄の追加や「トラックバック」の表示などが追加されています。

*「Default_Theme(英語版)」の「単体記事用テンプレート(single.php)」をコメント文(//説明文)を使って機能解説してみましょう。

Default_Theme/single.php

<?php
/**
 * @package WordPress
 * @subpackage Default_Theme
 */

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

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

    //コンテンツデータを取得する・ループのPHPコードの開始
	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        //投稿前後のナビゲーションの表示
		<div class="navigation">
			<div class="alignleft"><?php previous_post_link('&amp;laquo; %link') ?></div>
			<div class="alignright"><?php next_post_link('%link &amp;raquo;') ?></div>
		</div>

		<div <?php post_class() ?> id="post-<?php the_ID(); ?>">

           //投稿(コンテンツ)タイトル名の表示
		    <h2><?php the_title(); ?></h2>

			<div class="entry">

                //投稿(コンテンツ)内容の表示
				<?php the_content('<p class="serif">Read the rest of this entry &amp;raquo;</p>'); ?>

                //記事分割の際のリンク表示
				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>

                //投稿(コンテンツ)に付けられた「タグ」の表示
				<?php the_tags( '<p>Tags: ', ', ', '</p>'); ?>

                //投稿(コンテンツ)の履歴記録(日付、カテゴリー、RSS)メッセージの表示
				<p class="postmetadata alt">
					<small>
						This entry was posted
						<?php /* This is commented, because it requires a little adjusting sometimes.
							You'll need to download this plugin, and follow the instructions:
							http://binarybonsai.com/archives/2004/08/17/time-since-plugin/ */
							/* $entry_datetime = abs(strtotime($post->post_date) - (60*120)); echo time_since($entry_datetime); echo ' ago'; */ ?>
						on <?php the_time('l, F jS, Y') ?> at <?php the_time() ?>
						and is filed under <?php the_category(', ') ?>.
						You can follow any responses to this entry through the <?php post_comments_feed_link('RSS 2.0'); ?> feed.

                //コメント、トラックバック/ピンバックに関するメッセージの表示
						<?php if (('open' == $post-> comment_status) &amp;&amp; ('open' == $post->ping_status)) {
							// Both Comments and Pings are open ?>
							You can <a href="#respond">leave a response</a>, or <a href="<?php trackback_url(); ?>" rel="trackback">trackback</a> from your own site.

						<?php } elseif (!('open' == $post-> comment_status) &amp;&amp; ('open' == $post->ping_status)) {
							// Only Pings are Open ?>
							Responses are currently closed, but you can <a href="<?php trackback_url(); ?> " rel="trackback">trackback</a> from your own site.

						<?php } elseif (('open' == $post-> comment_status) &amp;&amp; !('open' == $post->ping_status)) {
							// Comments are open, Pings are not ?>
							You can skip to the end and leave a response. Pinging is currently not allowed.

						<?php } elseif (!('open' == $post-> comment_status) &amp;&amp; !('open' == $post->ping_status)) {
							// Neither Comments, nor Pings are open ?>
							Both comments and pings are currently closed.

						<?php } edit_post_link('Edit this entry','','.'); ?>

					</small>
				</p>

			</div>
		</div>

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

	<?php endwhile; else: ?>          //コンテンツデータがないときのメッセージ表示

		<p>Sorry, no posts matched your criteria.</p>

<?php endif; ?>          //コンテンツデータを取得する・ループのPHPコードの終了

	</div>

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

関連情報:

  1. Default/comments.php

  2. Default/archive.php

  3. Default/page.php

  4. Default/index.php

  5. category.php

  6. Default/sidebar.php

  7. テーマ(Theme)

2009.04.14 03;13

« | »

⇒この記事を印刷する

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

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





このページのトップへ

ewb_blue-l_160170.png
art-img.gif