WordPressに関する情報や技術紹介です

Completion of an individual article templates

(Single.php) has been completed template of an individual article.
As one is now complete.
I will leave page and contact us.

As for the template of an individual article, but I thought trying to create a page called "single-XX.php" in each category initially, to come to each page is hard to change when I'm getting more was summarized in the "single.php" by performing a conditional branch and in part by the replacement of, basic template so nearly together.

The difference in each category are the following two points.

  1. Image of the title is different in every category.
  2. If there is no parent category is without a notation of subcategories.

I had to do the following the "single.php" Keeping this in mind.

<?php

/* 変数の宣言 */
$img = ""; //画像のURL
$vis = "display: block"; //サブカテゴリの表示、非表示を設定するスタイル

/* カテゴリデータの取得 */
$cat_now = get_the_category();
$cat_now = $cat_now[0];

/*
 カテゴリIDの取得
 親カテゴリがある場合は親カテゴリのIDを取得
 親カテゴリがない場合は所属するカテゴリIDを取得
*/
$parent_id = $cat_now->category_parent;

if($parent_id == 0) {
	//親カテゴリが無い場合は現在のカテゴリIDを取得
	$parent_id = $cat_now->cat_ID;
	//サブカテゴリは非表示にする
	$vis = "display: none;";
}

//カテゴリIDによって画像URLを取得
switch($parent_id) {
	case 3:
		$img = "http://www.is-p.cc/wp-content/uploads/2008/04/wordpress01.jpg";
		break;
	case 6:
		$img = "http://www.is-p.cc/wp-content/uploads/2008/05/info01.jpg";
		break;
	default:
		$img = "";
}

?>

これで必要なタイトル画像URLとサブカテゴリの有無による表記の表示・非表示の情報が取得できました。
後は、続けて「single.php」に以下のようにすれば各カテゴリ毎に適切なタイトル画像とサブカテゴリについての表示・非表示が設定できます。

<h2><img src="<?php echo $img; ?>" width="600" height="220" /></h2>

<div style="<?php echo $vis; ?>">
<h3 class="title"><?php echo $cat_now->cat_name; ?></h3>
<p><?php echo $cat_now->category_description; ?></p>
</div>
タグ
,
トラックバックURL
  • [WordPress]single.phpでパンくずリスト

    single.phpで、 (prefix) » カテゴリー1 » カテゴリー2 » カテゴリー3 » 件名 というようなパンくずを出したい。 パンくずリスト作成のために、Breadcrumb Navigation XTを使用。 ↓ カ…

Leave a Reply

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

ページTOPに戻る