- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
「条件分岐タグ」の版間の差分
2行目: | 2行目: | ||
== Introduction == | == Introduction == | ||
− | + | 条件タグ(Conditional Tag)はテンプレートファイル内で表示される内容、表示させる対象ページを設定するのに使います。例えば、ブログのホームページの上部に短文を表示させたいとしましょう。<tt>[[#The Main Page|is_home()]]</tt>を使うことによって、その作業量は軽減できます。 | |
− | + | これらのタグは[[Template Hierarchy]]と近い関係にあります。 | |
==The Conditions For...== | ==The Conditions For...== | ||
− | All of the Conditional Tags test to see whether a certain condition is met, and then returns either TRUE or FALSE. '''The conditions under which various tags output TRUE is listed below'''. Those tags which can accept parameters are so noted. | + | 条件タグはその状況下であるかをチェックし、TRUE もしくは FALSE を返します。 |
+ | |||
+ | <!--All of the Conditional Tags test to see whether a certain condition is met, and then returns either TRUE or FALSE. '''The conditions under which various tags output TRUE is listed below'''. Those tags which can accept parameters are so noted.--> | ||
=== The Main Page === | === The Main Page === | ||
− | ; <tt>is_home()</tt> : | + | ; <tt>is_home()</tt> : メインページ(ブログのホームページ)が表示されている場合 |
=== A Single Post Page === | === A Single Post Page === | ||
− | ; <tt>is_single()</tt> : | + | ; <tt>is_single()</tt> : 個別記事のページが表示されている場合 |
− | ; <tt>is_single('17')</tt> : | + | ; <tt>is_single('17')</tt> : ID 17の記事が表示されている場合 |
− | ; <tt>is_single('Beef Stew')</tt> : | + | ; <tt>is_single('Beef Stew')</tt> : "Beef Stew"(ビーフシチュー)というタイトルの記事が表示されている場合 |
− | ; <tt>is_single('beef-stew')</tt> : | + | ; <tt>is_single('beef-stew')</tt> : "beef-stew"(ビーフシチュー)という投稿スラッグの記事が表示されている場合 |
=== A PAGE Page === | === A PAGE Page === | ||
− | + | ここでいうページとは、WordPressにおける[[Pages|ページ]]であり、いわゆる「ページ」とは異なります。その詳説については、「[[Pages#用語について|用語について]]」をご覧ください。 | |
− | ; <tt>is_page()</tt> : | + | ; <tt>is_page()</tt> : [[Pages|ページ]]が表示されている場合 |
− | ; <tt>is_page('42')</tt> : | + | ; <tt>is_page('42')</tt> : ID 42の[[Pages|ページ]]が表示されている場合 |
− | ; <tt>is_page('About Me')</tt> : | + | ; <tt>is_page('About Me')</tt> : "About Me"というタイトルの[[Pages|ページ]]が表示されている場合 |
− | ; <tt>is_page('about-me')</tt> : | + | ; <tt>is_page('about-me')</tt> : "about-me"という投稿スラッグの[[Pages|ページ]]が表示されている場合 |
− | + | 残念ながら、<tt>is_subpage()</tt>というタグは今のところありません。ですが、対応策はあります。 | |
− | ; <tt>if(get_the_title($post->post_parent) != the_title(' ' , ' ',false)) { echo "This is a subpage"; }</tt> : | + | ; <tt>if(get_the_title($post->post_parent) != the_title(' ' , ' ',false)) { echo "This is a subpage"; }</tt> : 表示中のページがサブページであるかどうかを親ページが存在するかどうかで判定します。サブページではない場合、それは親ページであることを意味します。この例文の場合、"This is a subpage"とブラウザー上に表示されます。 |
=== A Category Page === | === A Category Page === | ||
− | ; <tt>is_category()</tt> : | + | ; <tt>is_category()</tt> : 或るカテゴリーのアーカイヴページが表示されている場合 |
− | ; <tt>is_category('6')</tt> : | + | ; <tt>is_category('6')</tt> : カテゴリーID 6のアーカイヴページが表示されている場合 |
− | ; <tt>is_category('Cheeses')</tt> : | + | ; <tt>is_category('Cheeses')</tt> : "Cheeses"というカテゴリーのアーカイヴページが表示されている場合 |
− | ; <tt>is_category('cheeses')</tt> : | + | ; <tt>is_category('cheeses')</tt> : "cheeses"というカテゴリースラッグのアーカイヴページが表示されている場合 |
− | ; <tt>in_category('5')</tt> : | + | ; <tt>in_category('5')</tt> : カテゴリーID 5のアーカイヴページが表示されている場合に'''TRUEを返します''' |
− | + | ||
− | + | <tt>[[#Any Archive Page|is_archive()]]</tt>と[[Category Templates]]も参照してください。 | |
=== An Author Page === | === An Author Page === | ||
− | ; <tt>is_author()</tt> : | + | ; <tt>is_author()</tt> : ある著者のアーカイヴページが表示されている場合 |
− | ; <tt>is_author('1337')</tt> : | + | ; <tt>is_author('1337')</tt> : ID 1337の著者のアーカイヴページを表示している場合 |
− | ; <tt>is_author('Elite Hacker')</tt> : | + | ; <tt>is_author('Elite Hacker')</tt> : ニックネームが"Elite Hacker"のアーカイヴページを表示している場合 |
; <tt>is_author('elite-hacker')</tt> : When the archive page for the Author with Nicename "elite-hacker" is being displayed. | ; <tt>is_author('elite-hacker')</tt> : When the archive page for the Author with Nicename "elite-hacker" is being displayed. | ||
− | + | ||
+ | <tt>[[#Any Archive Page|is_archive()]]</tt>や[[Author Templates]]も併せてどうぞ。 | ||
=== A Date Page === | === A Date Page === | ||
− | ; <tt>is_date()</tt> : | + | ; <tt>is_date()</tt> : あらゆる日付別のアーカイヴページが表示されている場合(例:月別、年別、日別、時間別) |
− | ; <tt>is_year()</tt> : | + | ; <tt>is_year()</tt> : 年別のアーカイヴページが表示されている場合 |
− | ; <tt>is_month()</tt> : | + | ; <tt>is_month()</tt> : 月別のアーカイヴページが表示されている場合 |
− | ; <tt>is_day()</tt> : | + | ; <tt>is_day()</tt> : 日別のアーカイヴページが表示されている場合 |
− | ; <tt>is_time()</tt> : | + | ; <tt>is_time()</tt> : 毎時別、毎分別、毎秒別のアーカイヴページが表示されている場合 |
− | + | ||
+ | <tt>[[#Any Archive Page|is_archive()]]</tt>もどうぞ。 | ||
=== Any Archive Page === | === Any Archive Page === | ||
− | ; <tt>is_archive()</tt> : | + | ; <tt>is_archive()</tt> : あらゆるタイプのアーカイヴページが表示されている場合 |
=== A Search Result Page === | === A Search Result Page === | ||
− | ; <tt>is_search()</tt> : | + | ; <tt>is_search()</tt> : 検索結果のページが表示されている場合 |
=== A 404 Not Found Page === | === A 404 Not Found Page === | ||
− | ; <tt>is_404()</tt> : | + | ; <tt>is_404()</tt> : "HTTP 404: Not Found"エラーページが表示されている場合 |
=== A Paged Page === | === A Paged Page === | ||
85行目: | 87行目: | ||
=== A Trackback === | === A Trackback === | ||
− | ; <tt>is_trackback()</tt> : | + | ; <tt>is_trackback()</tt> : フックの中にトラックバックの機構が組み込まれている場合 |
+ | :このタグは一般のユーザーの為のものでは無く、WordPress内部で使われるものであり、プラグイン開発の際に使用されます。 | ||
==Working Examples== | ==Working Examples== | ||
− | + | 以下は条件タグの動作サンプルです。 | |
===Single Post=== | ===Single Post=== | ||
− | + | 以下は<tt>is_single()</tt>によって特定の情報を個別記事のページでのみ表示させる為の例文です。 | |
+ | |||
<pre> | <pre> | ||
if (is_single()) | if (is_single()) | ||
104行目: | 108行目: | ||
===Date-Based Differences=== | ===Date-Based Differences=== | ||
− | + | 以下の例は、日付別ページへのアクセスが行われたときに、年別に色分けして表示させます。 | |
<pre> | <pre> | ||
137行目: | 141行目: | ||
===Variable Sidebar Content=== | ===Variable Sidebar Content=== | ||
− | + | 以下の例は、アクセス中のページによってサイドバーに表示させる内容を変更させます。 | |
<pre> | <pre> | ||
184行目: | 188行目: | ||
===Helpful 404 page=== | ===Helpful 404 page=== | ||
− | + | ||
+ | 来訪者にとって404エラーページは不快で役立たずなものでしか在りません。WordPressでは、404ページをもっと役に立つページにすることが出来ます。例えば、ユーザーが存在しないページをクリックした時にメールで通知する方法です。これを使うときには、'''メールアドレスの設定'''を忘れずに行ってください。 | ||
<pre> | <pre> | ||
221行目: | 226行目: | ||
===Dynamic Menu Highlighting=== | ===Dynamic Menu Highlighting=== | ||
− | + | [[Dynamic Menu Highlighting|ダイナミックハイライト]]とは、条件タグを使用して、メニュー内の現在アクセスしているページを際立たせる方法です。 | |
{{No Param Tag Footer}} | {{No Param Tag Footer}} |
2006年8月31日 (木) 20:44時点における版
Introduction
条件タグ(Conditional Tag)はテンプレートファイル内で表示される内容、表示させる対象ページを設定するのに使います。例えば、ブログのホームページの上部に短文を表示させたいとしましょう。is_home()を使うことによって、その作業量は軽減できます。
これらのタグはTemplate Hierarchyと近い関係にあります。
The Conditions For...
条件タグはその状況下であるかをチェックし、TRUE もしくは FALSE を返します。
The Main Page
- is_home()
- メインページ(ブログのホームページ)が表示されている場合
A Single Post Page
- is_single()
- 個別記事のページが表示されている場合
- is_single('17')
- ID 17の記事が表示されている場合
- is_single('Beef Stew')
- "Beef Stew"(ビーフシチュー)というタイトルの記事が表示されている場合
- is_single('beef-stew')
- "beef-stew"(ビーフシチュー)という投稿スラッグの記事が表示されている場合
A PAGE Page
ここでいうページとは、WordPressにおけるページであり、いわゆる「ページ」とは異なります。その詳説については、「用語について」をご覧ください。
- is_page()
- ページが表示されている場合
- is_page('42')
- ID 42のページが表示されている場合
- is_page('About Me')
- "About Me"というタイトルのページが表示されている場合
- is_page('about-me')
- "about-me"という投稿スラッグのページが表示されている場合
残念ながら、is_subpage()というタグは今のところありません。ですが、対応策はあります。
- if(get_the_title($post->post_parent) != the_title(' ' , ' ',false)) { echo "This is a subpage"; }
- 表示中のページがサブページであるかどうかを親ページが存在するかどうかで判定します。サブページではない場合、それは親ページであることを意味します。この例文の場合、"This is a subpage"とブラウザー上に表示されます。
A Category Page
- is_category()
- 或るカテゴリーのアーカイヴページが表示されている場合
- is_category('6')
- カテゴリーID 6のアーカイヴページが表示されている場合
- is_category('Cheeses')
- "Cheeses"というカテゴリーのアーカイヴページが表示されている場合
- is_category('cheeses')
- "cheeses"というカテゴリースラッグのアーカイヴページが表示されている場合
- in_category('5')
- カテゴリーID 5のアーカイヴページが表示されている場合にTRUEを返します
is_archive()とCategory Templatesも参照してください。
An Author Page
- is_author()
- ある著者のアーカイヴページが表示されている場合
- is_author('1337')
- ID 1337の著者のアーカイヴページを表示している場合
- is_author('Elite Hacker')
- ニックネームが"Elite Hacker"のアーカイヴページを表示している場合
- is_author('elite-hacker')
- When the archive page for the Author with Nicename "elite-hacker" is being displayed.
is_archive()やAuthor Templatesも併せてどうぞ。
A Date Page
- is_date()
- あらゆる日付別のアーカイヴページが表示されている場合(例:月別、年別、日別、時間別)
- is_year()
- 年別のアーカイヴページが表示されている場合
- is_month()
- 月別のアーカイヴページが表示されている場合
- is_day()
- 日別のアーカイヴページが表示されている場合
- is_time()
- 毎時別、毎分別、毎秒別のアーカイヴページが表示されている場合
is_archive()もどうぞ。
Any Archive Page
- is_archive()
- あらゆるタイプのアーカイヴページが表示されている場合
A Search Result Page
- is_search()
- 検索結果のページが表示されている場合
A 404 Not Found Page
- is_404()
- "HTTP 404: Not Found"エラーページが表示されている場合
A Paged Page
- is_paged()
- When the page being displayed is "paged". This refers to an archive or the main page being split up over several pages. This does not refer to a Post or Page whose content has been divided into pages using the <!--nextpage--> QuickTag.
A Syndication
- is_feed()
- When the site requested is a Syndication. This tag is not typically used by users; it is used internally by WordPress and is available for Plugin Developers.
A Trackback
- is_trackback()
- フックの中にトラックバックの機構が組み込まれている場合
- このタグは一般のユーザーの為のものでは無く、WordPress内部で使われるものであり、プラグイン開発の際に使用されます。
Working Examples
以下は条件タグの動作サンプルです。
Single Post
以下はis_single()によって特定の情報を個別記事のページでのみ表示させる為の例文です。
if (is_single()) { echo 'This is just one of many fabulous entries in the ' . single_cat_title() . ' category!'; }
Date-Based Differences
以下の例は、日付別ページへのアクセスが行われたときに、年別に色分けして表示させます。
<?php // this starts The Loop if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <h2 id="post-<?php the_ID(); ?>"> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> <?php the_title(); ?></a></h2> <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small> <?php // are we showing a date-based archive? if (is_date()) { if (date('Y') != get_the_date('Y')) { // this post was written in a previous year // so let's style the content using the "oldentry" class echo '<div class="oldentry">'; } else { echo '<div class="entry">'; } } else { echo '<div class="entry">'; } the_content('Read the rest of this entry »'); ?> </div>
Variable Sidebar Content
以下の例は、アクセス中のページによってサイドバーに表示させる内容を変更させます。
<!-- begin sidebar --> <div id="sidebar"> <?php // let's generate info appropriate to the page being displayed if (is_home()) { // we're on the home page, so let's show a list of all top-level categories echo "<ul>"; wp_list_cats('optionall=0&sort_column=name&list=1&children=0'); echo "</ul>"; } elseif (is_category()) { // we're looking at a single category view, so let's show _all_ the categories echo "<ul>"; wp_list_cats('optionall=1&sort_column=name&list=1&children=1&hierarchical=1'); echo "</ul>"; } elseif (is_single()) { // we're looking at a single page, so let's not show anything in the sidebar } elseif (is_page()) { // we're looking at a static page. Which one? if (is_page('About')) { // our about page. echo "<p>This is my about page!</p>"; } elseif (is_page('Colophon')) { echo "<p>This is my colophon page, running on WordPress " . bloginfo('version') . "</p>"; } else { // catch-all for other pages echo "<p>Vote for Pedro!</p>"; } } else { // catch-all for everything else (archives, searches, 404s, etc) echo "<p>Pedro offers you his protection.</p>"; } // That's all, folks! ?> <form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <div> <input type="text" name="s" id="s" size="15" /> <input type="submit" value="<?php _e('Search'); ?>" /> </div> </form> </div> <!-- end sidebar -->
Helpful 404 page
来訪者にとって404エラーページは不快で役立たずなものでしか在りません。WordPressでは、404ページをもっと役に立つページにすることが出来ます。例えば、ユーザーが存在しないページをクリックした時にメールで通知する方法です。これを使うときには、メールアドレスの設定を忘れずに行ってください。
<p>You <?php #some variables for the script to use #if you have some reason to change these, do. but wordpress can handle it $adminemail = get_bloginfo('admin_email'); #the administrator email address, according to wordpress $website = get_bloginfo('url'); #gets your blog's url from wordpress $websitename = get_bloginfo('name'); #sets the blog's name, according to wordpress if (!isset($_SERVER['HTTP_REFERER'])) { #politely blames the user for all the problems they caused echo "tried going to "; #starts assembling an output paragraph $casemessage = "All is not lost!"; } elseif (isset($_SERVER['HTTP_REFERER'])) { #this will help the user find what they want, and email me of a bad link echo "clicked a link to"; #now the message says You clicked a link to... #setup a message to be sent to me $failuremess = "A user tried to go to $website" .$_SERVER['REQUEST_URI']." and received a 404 (page not found) error. "; $failuremess .= "It wasn't their fault, so try fixing it. They came from ".$_SERVER['HTTP_REFERER']; mail($adminemail, "Bad Link To ".$_SERVER['REQUEST_URI'], $failuremess, "From: $websitename <noreply@$website>"); #email you about problem $casemessage = "An administrator has been emailed about this problem, too.";#set a friendly message } echo " ".$website.$_SERVER['REQUEST_URI']; ?> and it doesn't exist. <?php echo $casemessage; ?> You can click back and try again or search for what you're looking for: <?php include(TEMPLATEPATH . "/searchform.php"); ?> </p>
Dynamic Menu Highlighting
ダイナミックハイライトとは、条件タグを使用して、メニュー内の現在アクセスしているページを際立たせる方法です。