- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
「条件分岐タグ」の版間の差分
(→A Category Page) |
|||
27行目: | 27行目: | ||
ここでいうページとは、WordPressにおける[[Pages|ページ]]であり、いわゆる「ページ」とは異なります。その詳説については、「[[Pages#用語について|用語について]]」をご覧ください。 | ここでいうページとは、WordPressにおける[[Pages|ページ]]であり、いわゆる「ページ」とは異なります。その詳説については、「[[Pages#用語について|用語について]]」をご覧ください。 | ||
− | ; <tt>is_page()</tt> : [[Pages| | + | ; <tt>is_page()</tt> : [[Pages|ペぼジ]]が表示されている場合 |
; <tt>is_page('42')</tt> : ID 42の[[Pages|ページ]]が表示されている場合 | ; <tt>is_page('42')</tt> : ID 42の[[Pages|ページ]]が表示されている場合 | ||
; <tt>is_page('About Me')</tt> : "About Me"というタイトルの[[Pages|ページ]]が表示されている場合 | ; <tt>is_page('About Me')</tt> : "About Me"というタイトルの[[Pages|ページ]]が表示されている場合 | ||
42行目: | 42行目: | ||
; <tt>is_category('Cheeses')</tt> : "Cheeses"というカテゴリーのアーカイヴページが表示されている場合 | ; <tt>is_category('Cheeses')</tt> : "Cheeses"というカテゴリーのアーカイヴページが表示されている場合 | ||
; <tt>is_category('cheeses')</tt> : "cheeses"というカテゴリースラッグのアーカイヴページが表示されている場合 | ; <tt>is_category('cheeses')</tt> : "cheeses"というカテゴリースラッグのアーカイヴページが表示されている場合 | ||
− | ; <tt>in_category('5')</tt> : | + | ; <tt>in_category('5')</tt> : 現在の記事がカテゴリーID 5に'''属する'''場合にtrueを返します。[[Template Tags/in category|詳細]] |
+ | <!-- Original Start | ||
+ | ; <tt>is_category()</tt> : When any Category archive page is being displayed. | ||
+ | ; <tt>is_category('6')</tt> : When the archive page for Category 6 is being displayed. | ||
+ | ; <tt>is_category('Cheeses')</tt> : When the archive page for the Category with Name "Cheeses" is being displayed. | ||
+ | ; <tt>is_category('cheeses')</tt> : When the archive page for the Category with Category Slug "cheeses" is being displayed. | ||
+ | ; <tt>in_category('5')</tt> : Returns true if the current post is '''in''' the specified category id. [http://codex.wordpress.org/Template_Tags/in_category read more] | ||
+ | Original End --> | ||
<tt>[[#Any Archive Page|is_archive()]]</tt>と[[Category Templates]]も参照してください。 | <tt>[[#Any Archive Page|is_archive()]]</tt>と[[Category Templates]]も参照してください。 |
2006年10月8日 (日) 21:51時点における版
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
ダイナミックハイライトとは、条件タグを使用して、メニュー内の現在アクセスしているページを際立たせる方法です。