- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
「テンプレートタグ/wp list pages」の版間の差分
(Old(パラメータの追加が必要)) |
(2.7, 2.8: パラメータ追加) |
||
1行目: | 1行目: | ||
− | {{ | + | {{NeedTrans|一部}} |
− | + | ||
{{Notice|疑問|パラメータ '''hierarchical''' の説明に疑問があります。||hierarchy について}} | {{Notice|疑問|パラメータ '''hierarchical''' の説明に疑問があります。||hierarchy について}} | ||
8行目: | 7行目: | ||
テンプレートタグ '''wp_list_pages()''' は WordPress [[Pages|ページ]]へのリンクのリスト (以下ページリストとする) を表示します。しばしば[[サイドバーのカスタマイズ|サイドバー]]/[[:en:Customizing_Your_Sidebar|en]] や[[Designing_Headers|ヘッダー]]/[[:en:Designing_Headers|en]] をカスタマイズするのに使われますが、他の[[テンプレート]]にも同様に使えるかもしれません。 | テンプレートタグ '''wp_list_pages()''' は WordPress [[Pages|ページ]]へのリンクのリスト (以下ページリストとする) を表示します。しばしば[[サイドバーのカスタマイズ|サイドバー]]/[[:en:Customizing_Your_Sidebar|en]] や[[Designing_Headers|ヘッダー]]/[[:en:Designing_Headers|en]] をカスタマイズするのに使われますが、他の[[テンプレート]]にも同様に使えるかもしれません。 | ||
− | |||
− | |||
<div id="Usage"> | <div id="Usage"> | ||
== 使い方 == | == 使い方 == | ||
</div> | </div> | ||
− | <?php wp_list_pages(' | + | |
+ | <?php wp_list_pages('[[#パラメータ|引数]]'); ?> | ||
<div id="Examples"> | <div id="Examples"> | ||
23行目: | 21行目: | ||
=== 初期設定での使用 === | === 初期設定での使用 === | ||
</div> | </div> | ||
− | <pre> | + | <pre>$defaults = array( |
− | $defaults = array( | + | 'depth' => 0, |
− | 'depth' => 0, | + | |
'show_date' => '', | 'show_date' => '', | ||
'date_format' => get_option('date_format'), | 'date_format' => get_option('date_format'), | ||
− | 'child_of' => 0, | + | 'child_of' => 0, |
'exclude' => '', | 'exclude' => '', | ||
− | 'title_li' => __('Pages'), | + | 'title_li' => __('Pages'), |
'echo' => 1, | 'echo' => 1, | ||
'authors' => '', | 'authors' => '', | ||
− | 'sort_column' => 'menu_order, post_title'); | + | 'sort_column' => 'menu_order, post_title', |
− | </pre> | + | 'link_before' => '', |
+ | 'link_after' => '', | ||
+ | 'exclude_tree'=> '' );</pre> | ||
デフォルトでは以下のように動作します。 | デフォルトでは以下のように動作します。 | ||
49行目: | 48行目: | ||
* すべてのページが含まれます (上述の例には載っていません) | * すべてのページが含まれます (上述の例には載っていません) | ||
* 特定のメタキーやメタ情報による制限はありません (上述の例には載っていません) | * 特定のメタキーやメタ情報による制限はありません (上述の例には載っていません) | ||
+ | * No Parent/Child trees excluded | ||
wp_list_pages(); | wp_list_pages(); | ||
58行目: | 58行目: | ||
''wp_list_pages'' が表示するページリストのデフォルトの見出し (「ページ」) は '''title_li''' パラメータに空の値を入れると表示しなくすることができます。以下の例はページリストの上に見出しのテキストを表示しません。 | ''wp_list_pages'' が表示するページリストのデフォルトの見出し (「ページ」) は '''title_li''' パラメータに空の値を入れると表示しなくすることができます。以下の例はページリストの上に見出しのテキストを表示しません。 | ||
− | < | + | <pre><ul> |
− | + | <?php wp_list_pages('title_li='); ?> | |
− | < | + | </ul></pre> |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </ | + | |
下記の例では ID が 9, 5, 23 のページだけを表示し、見出しのテキストを「Poetry」に置き換え、タグを <tt><nowiki><h2></nowiki></tt> に変更しています。 | 下記の例では ID が 9, 5, 23 のページだけを表示し、見出しのテキストを「Poetry」に置き換え、タグを <tt><nowiki><h2></nowiki></tt> に変更しています。 | ||
− | < | + | <pre><ul> |
− | + | <?php wp_list_pages('include=5,9,23&title_li=<h2>' . __('Poetry') . '</h2>' ); ?> | |
− | < | + | </ul></pre> |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </ | + | |
<div id="List_Pages_by_Page_Order"> | <div id="List_Pages_by_Page_Order"> | ||
82行目: | 72行目: | ||
</div> | </div> | ||
− | 下の例では [[管理パネル# | + | 下の例では [[管理パネル#Pages|ページ]] > [[Pages Add New SubPanel|新規追加]] で設定された'''ページ順序'''に従ってページリストを表示します。 |
− | < | + | <pre><ul> |
− | + | <?php wp_list_pages('sort_column=menu_order'); ?> | |
− | < | + | </ul></pre> |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </ | + | |
もしページ順序に従ってページリストを表示し、サイドバーに表示される見出しを「Prose」 (h2 タグスタイルで) と表示したい場合は、下のコードを sidebar.php ファイルに追加してください (サイドバーウィジェットを使用している場合は管理画面から)。 | もしページ順序に従ってページリストを表示し、サイドバーに表示される見出しを「Prose」 (h2 タグスタイルで) と表示したい場合は、下のコードを sidebar.php ファイルに追加してください (サイドバーウィジェットを使用している場合は管理画面から)。 | ||
− | < | + | <pre><ul> |
− | + | <?php wp_list_pages('sort_column=menu_order&title_li=<h2>' . __('Prose') . '</h2>' ); ?> | |
− | < | + | </ul></pre> |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </ | + | |
以下のコードを使うと、見出しなしで''ページ順序''に従い表示します。 | 以下のコードを使うと、見出しなしで''ページ順序''に従い表示します。 | ||
− | < | + | <pre><ul> |
− | + | <?php wp_list_pages('sort_column=menu_order&title_li='); ?> | |
− | < | + | </ul></pre> |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </ | + | |
<div id="Sort_Pages_by_Post_Date"> | <div id="Sort_Pages_by_Post_Date"> | ||
120行目: | 95行目: | ||
この例はページの作成日でソートし、その日付をページごとに表示します。 | この例はページの作成日でソートし、その日付をページごとに表示します。 | ||
− | < | + | <pre><ul> |
− | + | <?php wp_list_pages('sort_column=post_date&show_date=created'); ?> | |
− | < | + | </ul></pre> |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </ | + | |
<div id="Exclude_Pages_from_List"> | <div id="Exclude_Pages_from_List"> | ||
=== 特定のページを除外する === | === 特定のページを除外する === | ||
</div> | </div> | ||
− | '''exclude''' パラメータを使うと、''wp_list_pages'' | + | '''exclude''' パラメータを使うと、''wp_list_pages'' が表示するページリストから特定のページを除外することができます。 |
<!-- '''Note''': Page numbers must be in ascending order (i.e. <tt>'exclude=17,38'</tt> and not <tt>'exclude=38,17'</tt>). Note this isn't true at least as 2.1.3 --> | <!-- '''Note''': Page numbers must be in ascending order (i.e. <tt>'exclude=17,38'</tt> and not <tt>'exclude=38,17'</tt>). Note this isn't true at least as 2.1.3 --> | ||
− | < | + | <pre><ul> |
− | + | <?php wp_list_pages('exclude=17,38' ); ?> | |
− | < | + | </ul></pre> |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </ | + | |
<div id="Include_Pages_in_List"> | <div id="Include_Pages_in_List"> | ||
151行目: | 116行目: | ||
<!--'''Note''': Page numbers must be in ascending order (i.e. <tt>'include=7,13,26,35'</tt> and not <tt>'include=35,7,26,13'</tt>). this does not seem to be true at least at 2.1.3--> | <!--'''Note''': Page numbers must be in ascending order (i.e. <tt>'include=7,13,26,35'</tt> and not <tt>'include=35,7,26,13'</tt>). this does not seem to be true at least at 2.1.3--> | ||
− | < | + | <pre><ul> |
− | + | <?php wp_list_pages('include=7,13,26,35&title_li=<h2>' . __('Pages') . '</h2>' ); ?> | |
− | < | + | </ul> |
− | + | </pre> | |
− | + | ||
− | + | ||
− | + | ||
− | </ | + | |
<div id="List_Sub-Pages_.28versions_prior_to_Wordpress_2.0.1.29"> | <div id="List_Sub-Pages_.28versions_prior_to_Wordpress_2.0.1.29"> | ||
165行目: | 126行目: | ||
使用中の WordPress のテーマの page.php テンプレートの <tt>the_post()</tt> セクション内の <tt>the_content()</tt> の後にこれを追加するか、サブページがあるページの page.php テンプレートのコピーに追加します。 | 使用中の WordPress のテーマの page.php テンプレートの <tt>the_post()</tt> セクション内の <tt>the_content()</tt> の後にこれを追加するか、サブページがあるページの page.php テンプレートのコピーに追加します。 | ||
− | < | + | <pre><ul> |
− | + | <?php | |
− | < | + | global $id; |
− | + | wp_list_pages("title_li=&child_of=$id&show_date=modified | |
− | + | &date_format=$date_format"); ?> | |
− | global $id; | + | </ul></pre> |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | wp_list_pages("title_li=&child_of=$id&show_date=modified | + | |
− | &date_format=$date_format"); ? | + | |
− | + | ||
− | + | ||
− | </ | + | |
− | < | + | <p class="important">注意: この例は WordPress 2.0.1 以降ではページテンプレートに追加しても $id というグローバル変数がセットされていないので機能しません。替わりに以下のコードを使用してください。</p> |
<div id="List_Sub-Pages_.28Wordpress_2.0.1_or_newer.29"> | <div id="List_Sub-Pages_.28Wordpress_2.0.1_or_newer.29"> | ||
188行目: | 140行目: | ||
注意: サブページがない場合でも、HTML タグ (<tt><nowiki><ul></nowiki></tt> や <tt><nowiki><ol></nowiki></tt>) が表示されます。スタイルシートを使ってリストのデザインをしている場合はこのことを覚えておいてください。 | 注意: サブページがない場合でも、HTML タグ (<tt><nowiki><ul></nowiki></tt> や <tt><nowiki><ol></nowiki></tt>) が表示されます。スタイルシートを使ってリストのデザインをしている場合はこのことを覚えておいてください。 | ||
− | < | + | <pre><ul> |
− | + | <?php | |
− | < | + | wp_list_pages('title_li=&child_of='.$post->ID.'&show_date=modified |
− | + | &date_format=$date_format'); ?> | |
− | + | </ul> | |
− | &date_format=$date_format'); ? | + | </pre> |
− | + | ||
− | + | ||
− | </ | + | |
以下の例は表示中のページに子ページ (表示中のページを親ページと設定しているページ) が存在する場合のみリストを表示します。 | 以下の例は表示中のページに子ページ (表示中のページを親ページと設定しているページ) が存在する場合のみリストを表示します。 | ||
− | < | + | <pre><?php |
− | + | $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0'); | |
− | < | + | if ($children) { ?> |
− | + | <ul> | |
− | $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0'); | + | <?php echo $children; ?> |
− | if ($children) { ? | + | </ul> |
− | + | <?php } ?> | |
− | + | </pre> | |
− | + | ||
− | + | ||
− | + | ||
− | </ | + | |
<div id="List_subpages_even_if_on_a_subpage"> | <div id="List_subpages_even_if_on_a_subpage"> | ||
218行目: | 163行目: | ||
上述の例では親ページでは子ページを表示しますが、子ページでは表示しません。次のコードは親ページでも子ページでも、子ページのリストを表示します。 | 上述の例では親ページでは子ページを表示しますが、子ページでは表示しません。次のコードは親ページでも子ページでも、子ページのリストを表示します。 | ||
− | + | (注) このコードは、サイドバーのウィジェットブロックの後ろに置くと動作しません。 | |
− | < | + | <pre><?php |
− | + | if($post->post_parent) | |
− | < | + | $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); |
− | + | else | |
− | if($post->post_parent) | + | $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); |
− | $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); | + | if ($children) { ?> |
− | else | + | <ul> |
− | $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); | + | <?php echo $children; ?> |
− | if ($children) { ? | + | </ul> |
− | + | <?php } ?> | |
− | + | </pre> | |
− | + | ||
− | + | As an alternative, this code in a ''sidebar.php'', displays only top level Pages, but when viewing a Page that has children (or is a child) it displays only children of that parent. | |
− | </ | + | * When visiting main page, all top level pages are listed in the sidebar. |
− | </ | + | * When visiting a top level page with no children, all top level pages are listed. |
+ | * When visiting a top level page with children, just the children pages, and descendant pages, are listed. | ||
+ | * When visiting a child page, just the children, and descendant pages, of that parent, are listed. | ||
+ | <pre> | ||
+ | <?php | ||
+ | $output = wp_list_pages('echo=0&depth=1&title_li=<h2>Top Level Pages </h2>' ); | ||
+ | if (is_page( )) { | ||
+ | $page = $post->ID; | ||
+ | if ($post->post_parent) { | ||
+ | $page = $post->post_parent; | ||
+ | } | ||
+ | $children=wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=' ); | ||
+ | if ($children) { | ||
+ | $output = wp_list_pages ('echo=0&child_of=' . $page . '&title_li=<h2>Child Pages</h2>'); | ||
+ | } | ||
+ | } | ||
+ | echo $output; | ||
+ | ?> | ||
+ | </pre> | ||
<div id="Markup_and_styling_of_page_items"> | <div id="Markup_and_styling_of_page_items"> | ||
=== ページアイテムのマークアップとデザイン === | === ページアイテムのマークアップとデザイン === | ||
</div> | </div> | ||
− | デフォルトでは、'''wp_list_pages()''' は [[管理パネル# | + | デフォルトでは、'''wp_list_pages()''' は [[管理パネル#Pages|ページ]] > [[Pages Add New SubPanel|新規追加]] で作成した WordPress ページを入れ子で順不同のリストに表示します。あなたは最外部のアイテム (<tt>li.pagenav</tt>) をなくすことも、<var>title_li</var> パラメータを空の値にすることもできます。 |
'''wp_list_pages()''' が表示するすべてのリストアイテム (<tt>li</tt>) には <tt>page_item</tt> というクラスが付けられています。'''wp_list_pages()''' をページ表示時に呼び出すと、そのページを指すリストアイテムには <tt>current_page_item</tt> というクラスが追加されます。 | '''wp_list_pages()''' が表示するすべてのリストアイテム (<tt>li</tt>) には <tt>page_item</tt> というクラスが付けられています。'''wp_list_pages()''' をページ表示時に呼び出すと、そのページを指すリストアイテムには <tt>current_page_item</tt> というクラスが追加されます。 | ||
− | <pre> | + | <pre><li class="pagenav"> |
− | <li class="pagenav"> | + | Pages [title_li] |
− | + | ||
<ul> | <ul> | ||
− | <li class="page_item current_page_parent"> | + | <!-- Output starts here if 'title_li' parameter is empty --> |
− | [parent of the current | + | <li class="page-item-2 page_item current_page_ancestor current_page_parent"> |
+ | [parent of the current Page] | ||
<ul> | <ul> | ||
− | <li class="page_item current_page_item"> | + | <li class="page-item-21 page_item current_page_item"> |
− | [the current | + | [the current Page] |
</li> | </li> | ||
</ul> | </ul> | ||
</li> | </li> | ||
− | <li class="page_item"> | + | <li class="page-item-3 page_item"> |
− | [another | + | [another Page] |
</li> | </li> | ||
</ul> | </ul> | ||
− | </li> | + | </li></pre> |
− | </pre> | + | |
これらは [[用語集#CSS|CSS セレクタ]]でデザインできます。 | これらは [[用語集#CSS|CSS セレクタ]]でデザインできます。 | ||
− | + | <pre>.pagenav { … } /* the outermost list item; contains whole list */ | |
− | + | .page-item-2 { … } /* item for Page ID 2 */ | |
− | + | .page_item { … } /* any Page item */ | |
− | + | .current_page_item { … } /* the current Page */ | |
+ | .current_page_parent { … } /* parent of the current Page */ | ||
+ | .current_page_ancestor { … } /* any ancestor of the current Page */ | ||
+ | </pre> | ||
<div id="Parameters"> | <div id="Parameters"> | ||
274行目: | 239行目: | ||
; '''sort_column''' : (''文字列'') ページのリストをいろいろな順序で並び替えます。デフォルトでは'ページタイトルのアルファベット順''に並び替えます | ; '''sort_column''' : (''文字列'') ページのリストをいろいろな順序で並び替えます。デフォルトでは'ページタイトルのアルファベット順''に並び替えます | ||
:* <tt>'post_title'</tt> - (タイトルを) アルファベット順に並び替えます - デフォルト | :* <tt>'post_title'</tt> - (タイトルを) アルファベット順に並び替えます - デフォルト | ||
− | :* <tt>'menu_order'</tt> - ページ順序によって並び替えます。''要注意'': ''ページ順序''と''ページ ID'' に注意してください。ページ ID はそれぞれの記事やページにつけられた唯一の番号です。ページ順序は [[ | + | :* <tt>'menu_order'</tt> - ページ順序によって並び替えます。''要注意'': ''ページ順序''と''ページ ID'' に注意してください。ページ ID はそれぞれの記事やページにつけられた唯一の番号です。ページ順序は [[管理パネル#Pages|ページ]] > [[Pages Add New SubPanel|新規追加]]画面でユーザーがつける番号のことです。[[#List Pages by Page Order|上述の例]]をお読みください |
:* <tt>'post_date'</tt> - 作成日順に並び替えます | :* <tt>'post_date'</tt> - 作成日順に並び替えます | ||
:* <tt>'post_modified'</tt> - 最終更新日順に並び替えます | :* <tt>'post_modified'</tt> - 最終更新日順に並び替えます | ||
280行目: | 245行目: | ||
:* <tt>'post_author'</tt> - ページ作成者の ID 順に並び替えます | :* <tt>'post_author'</tt> - ページ作成者の ID 順に並び替えます | ||
:* <tt>'post_name'</tt> - [[用語集#Slug|ページスラッグ]] (パーマリンク) のアルファベット順に並び替えます | :* <tt>'post_name'</tt> - [[用語集#Slug|ページスラッグ]] (パーマリンク) のアルファベット順に並び替えます | ||
− | + | : '''注意''': '''sort_column''' パラメータは WordPress のデータベースの [[データベース概要#Table:_wp_posts|wp_post テーブル]]内のあらゆるフィールドの記述子を使ってページのリストを並び替えることができます。 | |
− | '''注意''': '''sort_column''' パラメータは WordPress のデータベースの [[データベース概要#Table:_wp_posts|wp_post テーブル]]内のあらゆるフィールドの記述子を使ってページのリストを並び替えることができます。 | + | |
− | + | ||
; '''sort_order''' : (''文字列'') ページリストの並び順を変更します (昇順または降順)。デフォルトは''昇順''です | ; '''sort_order''' : (''文字列'') ページリストの並び順を変更します (昇順または降順)。デフォルトは''昇順''です | ||
290行目: | 253行目: | ||
; '''exclude''' : (''文字列'') リストから除外したいページの ID をコンマで区切ったリストで定義します (例: <tt>'exclude=3,7,31'</tt>)。デフォルト値はありません。[[#Exclude Pages from List|特定のページを除外する例]]をお読みください。 | ; '''exclude''' : (''文字列'') リストから除外したいページの ID をコンマで区切ったリストで定義します (例: <tt>'exclude=3,7,31'</tt>)。デフォルト値はありません。[[#Exclude Pages from List|特定のページを除外する例]]をお読みください。 | ||
− | ; ''' | + | ; '''exclude_tree''' |
+ | : (''文字列'') 除外したい親ページID をカンマ区切りで指定します。指定した親ページとその子ページ全てを除外するには、このパラメータを使います。<tt>'exclude_tree=5'</tt> とすれば、ページID 5 と、ページID 5 を親とする子ページ全て(子も孫も全て)が除外されます。 | ||
+ | : このパラメータは [[Version 2.7|バージョン 2.7]] 以上で使えます。 | ||
− | ; '''depth''' : (''整数'') このパラメータは ''wp_list_pages'' | + | ; '''include''' : (''文字列'') ''wp_list_pages'' で生成されるリストに特定のページだけを含めます。'''exclude''' と同様に、このパラメータもコンマで ID を区切ったリストで定義します。[[#Include Pages in List|特定のページだけを含める例]]をお読みください |
+ | |||
+ | ; '''depth''' : (''整数'') このパラメータは ''wp_list_pages'' で生成されるリストに含めるページの階層レベルを設定します。デフォルト値は '''0''' です (すべてのページとサブページを表示します)。 | ||
:* <tt>0</tt> - ページとサブページを親子関係を維持して (インデントして) 表示します (デフォルト) | :* <tt>0</tt> - ページとサブページを親子関係を維持して (インデントして) 表示します (デフォルト) | ||
:* <tt>-1</tt> - ページとサブページを同列に (インデントなし) 表示します | :* <tt>-1</tt> - ページとサブページを同列に (インデントなし) 表示します | ||
:* <tt>1</tt> - トップレベルのページのみ表示します | :* <tt>1</tt> - トップレベルのページのみ表示します | ||
:* <tt>2</tt> - 2 (またはそれ以上) の深さ (レベル) のページを表示します | :* <tt>2</tt> - 2 (またはそれ以上) の深さ (レベル) のページを表示します | ||
− | + | ||
− | ; '''child_of''' : (''整数'') ページ ID | + | ; '''child_of''' : (''整数'') ページ ID を値としてそのページのサブページのみを表示します。デフォルト値は '''0''' です (すべてのページを表示)。 |
; '''show_date''' : (''文字列'') それぞれのページの作成日または最終更新日を表示します。デフォルト値は''空''です (日付を表示しません)。 | ; '''show_date''' : (''文字列'') それぞれのページの作成日または最終更新日を表示します。デフォルト値は''空''です (日付を表示しません)。 | ||
305行目: | 272行目: | ||
:* <tt>'xxx'</tt> - ''modified'' 以外の値は作成日 ('''post_date''') を表示します。[[#Sort Pages by Post Date|上述の例]]をお読みください | :* <tt>'xxx'</tt> - ''modified'' 以外の値は作成日 ('''post_date''') を表示します。[[#Sort Pages by Post Date|上述の例]]をお読みください | ||
− | ; '''date_format''' : (''文字列'') '''show_date''' パラメータで使うページの日付の書式を設定します (例: "<tt><nowiki>l, F j, Y</nowiki></tt>")。このパラメータは WordPress の管理画面で設定した日付フォーマットをデフォルトとします。[[Formatting Date and Time|日付と時刻の書式]]と [http:// | + | ; '''date_format''' : (''文字列'') '''show_date''' パラメータで使うページの日付の書式を設定します (例: "<tt><nowiki>l, F j, Y</nowiki></tt>")。このパラメータは WordPress の管理画面で設定した日付フォーマットをデフォルトとします。[[Formatting Date and Time|日付と時刻の書式]]と [http://www.php.net/manual/ja/function.date.php PHPマニュアルの日時の書式化のページ]をお読みください |
− | ; '''title_li''' : (''文字列'') ページリストの見出しのテキストとスタイルを設定します。デフォルトは「<tt>__('Pages')</tt>」です。ブログ上では「ページ」と表示されます (<tt>__('')</tt> は[[WordPress の翻訳|ローカライゼーション]]のためのものです)。空の値のとき (<tt>''</tt>) は見出しを表示しません。また、リストは <tt> | + | ; '''title_li''' : (''文字列'') ページリストの見出しのテキストとスタイルを設定します。デフォルトは「<tt>__('Pages')</tt>」です。ブログ上では「ページ」と表示されます (<tt>__('')</tt> は[[WordPress の翻訳|ローカライゼーション]]のためのものです)。空の値のとき (<tt>''</tt>) は見出しを表示しません。また、リストは <tt><nowiki><ul></nowiki>, <nowiki></ul></nowiki></tt> タグで包まれていません。[[#Hiding or Changing the List Heading|見出しの例]]をお読みください |
; '''echo''' : (''論理値'') ページリストを表示するか、PHP で使うために HTML テキストとして返すかを設定します。デフォルト値は '''1''' (リストを表示) です。 | ; '''echo''' : (''論理値'') ページリストを表示するか、PHP で使うために HTML テキストとして返すかを設定します。デフォルト値は '''1''' (リストを表示) です。 | ||
320行目: | 287行目: | ||
; meta_value : (''文字列'') ここに記述したカスタムフィールド値を持つページだけを表示します (meta_key フィールドと同時に使います)。 | ; meta_value : (''文字列'') ここに記述したカスタムフィールド値を持つページだけを表示します (meta_key フィールドと同時に使います)。 | ||
+ | |||
+ | ; '''link_before''' | ||
+ | : (''文字列'') Sets the text or html that proceeds the link text inside <a> tag. (Version 2.7.0 or newer.) | ||
+ | ; '''link_after''' (''string''): Sets the text or html that follows the link text inside <a> tag. (Version 2.7.0 or newer.) | ||
+ | ; '''authors''' | ||
+ | : (''文字列'') Only include Pages authored by the authors in this comma-separated list of author IDs. If no authors are specified, all authors are included. | ||
+ | ; '''number''' | ||
+ | : (''文字列'') Sets the number of Pages to display. This causes the SQL LIMIT value to be defined. Default to no LIMIT. This parameter was added with [[Version 2.8]]. | ||
+ | ; '''offset''' | ||
+ | : (''文字列'') Then number of Pages to pass over (or displace) before collecting the set of Pages. Default is no OFFSET. This parameter was added with [[Version 2.8]]. | ||
+ | |||
+ | == Notes == | ||
+ | |||
+ | * See Also: [[テンプレートタグ/wp_page_menu|wp_page_menu()]] | ||
== 変更履歴 == | == 変更履歴 == | ||
+ | * [[Version 2.8|2.8]] : パラメータ 'number', 'offset' が追加されました。 | ||
+ | * [[Version 2.7|2.7]] : パラメータ 'link_before', 'link_after', 'exclude_tree' が追加されました。 | ||
* [[:Category:バージョン|1.5]] : 新規テンプレートタグ | * [[:Category:バージョン|1.5]] : 新規テンプレートタグ | ||
<div id="Related"> | <div id="Related"> | ||
− | |||
== 関連 == | == 関連 == | ||
</div> | </div> | ||
332行目: | 314行目: | ||
{{Query String Tag Footer}} | {{Query String Tag Footer}} | ||
− | {{原文|Template Tags/wp_list_pages| | + | {{原文|Template Tags/wp_list_pages|72974}}<!-- 12:50, May 29, 2009 Ramiy 版 --> |
{{DEFAULTSORT:Wp_list_pages}} | {{DEFAULTSORT:Wp_list_pages}} | ||
[[Category:wp2.7]] | [[Category:wp2.7]] | ||
+ | [[Category:wp2.8]] | ||
[[en:Template Tags/wp_list_pages]] | [[en:Template Tags/wp_list_pages]] |
2009年5月30日 (土) 18:00時点における版
このページ「テンプレートタグ/wp list pages」は一部未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています。
疑問: パラメータ hierarchical の説明に疑問があります。このページのノートをご覧ください。
目次
説明
テンプレートタグ wp_list_pages() は WordPress ページへのリンクのリスト (以下ページリストとする) を表示します。しばしばサイドバー/en やヘッダー/en をカスタマイズするのに使われますが、他のテンプレートにも同様に使えるかもしれません。
使い方
<?php wp_list_pages('引数'); ?>
用例
初期設定での使用
$defaults = array( 'depth' => 0, 'show_date' => '', 'date_format' => get_option('date_format'), 'child_of' => 0, 'exclude' => '', 'title_li' => __('Pages'), 'echo' => 1, 'authors' => '', 'sort_column' => 'menu_order, post_title', 'link_before' => '', 'link_after' => '', 'exclude_tree'=> '' );
デフォルトでは以下のように動作します。
- すべてのページとサブページを表示します (階層制限はありません)
- ページの作成日は表示しません
- すべてのページの子ページに関する制限はありません
- 除外されるページはありません
- リストの見出しが「ページ」となります
- 結果は echo されます (表示されます)
- ページの作成者による制限はありません
- ページのタイトル順にソートされます
- 昇順にソートされます (上述の例には載っていません)
- ページの親子関係をインデントして表示します (上述の例には載っていません)
- すべてのページが含まれます (上述の例には載っていません)
- 特定のメタキーやメタ情報による制限はありません (上述の例には載っていません)
- No Parent/Child trees excluded
wp_list_pages();
リストの見出しを隠す、変える
wp_list_pages が表示するページリストのデフォルトの見出し (「ページ」) は title_li パラメータに空の値を入れると表示しなくすることができます。以下の例はページリストの上に見出しのテキストを表示しません。
<ul> <?php wp_list_pages('title_li='); ?> </ul>
下記の例では ID が 9, 5, 23 のページだけを表示し、見出しのテキストを「Poetry」に置き換え、タグを <h2> に変更しています。
<ul> <?php wp_list_pages('include=5,9,23&title_li=<h2>' . __('Poetry') . '</h2>' ); ?> </ul>
リストの順序を変える
下の例では ページ > 新規追加 で設定されたページ順序に従ってページリストを表示します。
<ul> <?php wp_list_pages('sort_column=menu_order'); ?> </ul>
もしページ順序に従ってページリストを表示し、サイドバーに表示される見出しを「Prose」 (h2 タグスタイルで) と表示したい場合は、下のコードを sidebar.php ファイルに追加してください (サイドバーウィジェットを使用している場合は管理画面から)。
<ul> <?php wp_list_pages('sort_column=menu_order&title_li=<h2>' . __('Prose') . '</h2>' ); ?> </ul>
以下のコードを使うと、見出しなしでページ順序に従い表示します。
<ul> <?php wp_list_pages('sort_column=menu_order&title_li='); ?> </ul>
作成日順に並び替える
この例はページの作成日でソートし、その日付をページごとに表示します。
<ul> <?php wp_list_pages('sort_column=post_date&show_date=created'); ?> </ul>
特定のページを除外する
exclude パラメータを使うと、wp_list_pages が表示するページリストから特定のページを除外することができます。
<ul> <?php wp_list_pages('exclude=17,38' ); ?> </ul>
特定のページだけを含める
特定のページだけをリストに含めたい場合 (例えば ID が 35, 7 ,26, 13 のページ) は、include パラメータを使用します。
<ul> <?php wp_list_pages('include=7,13,26,35&title_li=<h2>' . __('Pages') . '</h2>' ); ?> </ul>
サブページの表示 (WordPress 2.0.1 以前)
使用中の WordPress のテーマの page.php テンプレートの the_post() セクション内の the_content() の後にこれを追加するか、サブページがあるページの page.php テンプレートのコピーに追加します。
<ul> <?php global $id; wp_list_pages("title_li=&child_of=$id&show_date=modified &date_format=$date_format"); ?> </ul>
注意: この例は WordPress 2.0.1 以降ではページテンプレートに追加しても $id というグローバル変数がセットされていないので機能しません。替わりに以下のコードを使用してください。
サブページの表示 (WordPress 2.0.1 以降)
注意: サブページがない場合でも、HTML タグ (<ul> や <ol>) が表示されます。スタイルシートを使ってリストのデザインをしている場合はこのことを覚えておいてください。
<ul> <?php wp_list_pages('title_li=&child_of='.$post->ID.'&show_date=modified &date_format=$date_format'); ?> </ul>
以下の例は表示中のページに子ページ (表示中のページを親ページと設定しているページ) が存在する場合のみリストを表示します。
<?php $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0'); if ($children) { ?> <ul> <?php echo $children; ?> </ul> <?php } ?>
サブページの中でもサブページのリストを表示する
上述の例では親ページでは子ページを表示しますが、子ページでは表示しません。次のコードは親ページでも子ページでも、子ページのリストを表示します。
(注) このコードは、サイドバーのウィジェットブロックの後ろに置くと動作しません。
<?php if($post->post_parent) $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); else $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); if ($children) { ?> <ul> <?php echo $children; ?> </ul> <?php } ?>
As an alternative, this code in a sidebar.php, displays only top level Pages, but when viewing a Page that has children (or is a child) it displays only children of that parent.
- When visiting main page, all top level pages are listed in the sidebar.
- When visiting a top level page with no children, all top level pages are listed.
- When visiting a top level page with children, just the children pages, and descendant pages, are listed.
- When visiting a child page, just the children, and descendant pages, of that parent, are listed.
<?php $output = wp_list_pages('echo=0&depth=1&title_li=<h2>Top Level Pages </h2>' ); if (is_page( )) { $page = $post->ID; if ($post->post_parent) { $page = $post->post_parent; } $children=wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=' ); if ($children) { $output = wp_list_pages ('echo=0&child_of=' . $page . '&title_li=<h2>Child Pages</h2>'); } } echo $output; ?>
ページアイテムのマークアップとデザイン
デフォルトでは、wp_list_pages() は ページ > 新規追加 で作成した WordPress ページを入れ子で順不同のリストに表示します。あなたは最外部のアイテム (li.pagenav) をなくすことも、title_li パラメータを空の値にすることもできます。
wp_list_pages() が表示するすべてのリストアイテム (li) には page_item というクラスが付けられています。wp_list_pages() をページ表示時に呼び出すと、そのページを指すリストアイテムには current_page_item というクラスが追加されます。
<li class="pagenav"> Pages [title_li] <ul> <!-- Output starts here if 'title_li' parameter is empty --> <li class="page-item-2 page_item current_page_ancestor current_page_parent"> [parent of the current Page] <ul> <li class="page-item-21 page_item current_page_item"> [the current Page] </li> </ul> </li> <li class="page-item-3 page_item"> [another Page] </li> </ul> </li>
これらは CSS セレクタでデザインできます。
.pagenav { … } /* the outermost list item; contains whole list */ .page-item-2 { … } /* item for Page ID 2 */ .page_item { … } /* any Page item */ .current_page_item { … } /* the current Page */ .current_page_parent { … } /* parent of the current Page */ .current_page_ancestor { … } /* any ancestor of the current Page */
パラメータ
- sort_column
- (文字列) ページのリストをいろいろな順序で並び替えます。デフォルトでは'ページタイトルのアルファベット順に並び替えます
- 'post_title' - (タイトルを) アルファベット順に並び替えます - デフォルト
- 'menu_order' - ページ順序によって並び替えます。要注意: ページ順序とページ ID に注意してください。ページ ID はそれぞれの記事やページにつけられた唯一の番号です。ページ順序は ページ > 新規追加画面でユーザーがつける番号のことです。上述の例をお読みください
- 'post_date' - 作成日順に並び替えます
- 'post_modified' - 最終更新日順に並び替えます
- 'ID' - ページ ID 順に並び替えます
- 'post_author' - ページ作成者の ID 順に並び替えます
- 'post_name' - ページスラッグ (パーマリンク) のアルファベット順に並び替えます
- 注意: sort_column パラメータは WordPress のデータベースの wp_post テーブル内のあらゆるフィールドの記述子を使ってページのリストを並び替えることができます。
- sort_order
- (文字列) ページリストの並び順を変更します (昇順または降順)。デフォルトは昇順です
- 'asc' - 低いほうから高いほうへ並び替えます
- 'desc' - 高いほうから低いほうへ並び替えます
- exclude
- (文字列) リストから除外したいページの ID をコンマで区切ったリストで定義します (例: 'exclude=3,7,31')。デフォルト値はありません。特定のページを除外する例をお読みください。
- exclude_tree
- (文字列) 除外したい親ページID をカンマ区切りで指定します。指定した親ページとその子ページ全てを除外するには、このパラメータを使います。'exclude_tree=5' とすれば、ページID 5 と、ページID 5 を親とする子ページ全て(子も孫も全て)が除外されます。
- このパラメータは バージョン 2.7 以上で使えます。
- include
- (文字列) wp_list_pages で生成されるリストに特定のページだけを含めます。exclude と同様に、このパラメータもコンマで ID を区切ったリストで定義します。特定のページだけを含める例をお読みください
- depth
- (整数) このパラメータは wp_list_pages で生成されるリストに含めるページの階層レベルを設定します。デフォルト値は 0 です (すべてのページとサブページを表示します)。
- 0 - ページとサブページを親子関係を維持して (インデントして) 表示します (デフォルト)
- -1 - ページとサブページを同列に (インデントなし) 表示します
- 1 - トップレベルのページのみ表示します
- 2 - 2 (またはそれ以上) の深さ (レベル) のページを表示します
- child_of
- (整数) ページ ID を値としてそのページのサブページのみを表示します。デフォルト値は 0 です (すべてのページを表示)。
- show_date
- (文字列) それぞれのページの作成日または最終更新日を表示します。デフォルト値は空です (日付を表示しません)。
- '' - 日付を表示しません (デフォルト)
- 'modified' - 最終更新日を表示します
- 'xxx' - modified 以外の値は作成日 (post_date) を表示します。上述の例をお読みください
- date_format
- (文字列) show_date パラメータで使うページの日付の書式を設定します (例: "l, F j, Y")。このパラメータは WordPress の管理画面で設定した日付フォーマットをデフォルトとします。日付と時刻の書式と PHPマニュアルの日時の書式化のページをお読みください
- title_li
- (文字列) ページリストの見出しのテキストとスタイルを設定します。デフォルトは「__('Pages')」です。ブログ上では「ページ」と表示されます (__('') はローカライゼーションのためのものです)。空の値のとき ('') は見出しを表示しません。また、リストは <ul>, </ul> タグで包まれていません。見出しの例をお読みください
- echo
- (論理値) ページリストを表示するか、PHP で使うために HTML テキストとして返すかを設定します。デフォルト値は 1 (リストを表示) です。
- 1 (true) - デフォルト
- 0 (false)
- hierarchical
- (論理値) サブページを親ページの下にインデントして表示するかインラインで表示するかを設定します。デフォルトは true です (サブページを親ページの下にインデントして表示)
- 1 (true) - デフォルト
- 0 (false)
- meta_key
- (文字列) ここに記述したカスタムフィールドキーを持つページだけを表示します (meta_value フィールドと同時に使います)。
- meta_value
- (文字列) ここに記述したカスタムフィールド値を持つページだけを表示します (meta_key フィールドと同時に使います)。
- link_before
- (文字列) Sets the text or html that proceeds the link text inside <a> tag. (Version 2.7.0 or newer.)
- link_after (string)
- Sets the text or html that follows the link text inside <a> tag. (Version 2.7.0 or newer.)
- authors
- (文字列) Only include Pages authored by the authors in this comma-separated list of author IDs. If no authors are specified, all authors are included.
- number
- (文字列) Sets the number of Pages to display. This causes the SQL LIMIT value to be defined. Default to no LIMIT. This parameter was added with Version 2.8.
- offset
- (文字列) Then number of Pages to pass over (or displace) before collecting the set of Pages. Default is no OFFSET. This parameter was added with Version 2.8.
Notes
- See Also: wp_page_menu()
変更履歴
- 2.8 : パラメータ 'number', 'offset' が追加されました。
- 2.7 : パラメータ 'link_before', 'link_after', 'exclude_tree' が追加されました。
- 1.5 : 新規テンプレートタグ
関連
記事
- 記事: ループ - WordPress ループ内でのクエリの使い方に関する基本的概要。
- 記事: クエリ概要 - どのクエリが WordPress で生成されているかを判断するための説明。
- 記事: フックを使ったクエリのカスタマイズ
- 記事: MYSQL カスタムセレクトクエリ /en
- 記事: 高度なタクソノミークエリ /en
- 記事: オフセット・ペジネーションを使ったカスタムクエリ /en
コードドキュメンテーション
- クラス: WP_Query - WP_Query クエリの詳細概要
- オブジェクト: $wpdb - $wpdb オブジェクトの使い方概要
- 関数: get_query_var()
- 関数: query_posts() - 追加カスタムクエリの作成
- 関数: get_post() - 項目の ID を使ってその投稿のレコードをデータベースから取得する
- 関数: get_posts() - 項目の配列を返す特別な関数。
- 関数: get_pages() - ページの配列を返す特別な関数。
- 関数: have posts() - クエリが記事を返すかどうか判断する条件分岐。
- 関数: the_post() - クエリの後に自動的にループを設定するのに使われる。
- 関数: rewind_posts() - 現在のループをクリアする。
- 関数: setup_postdata() - ループ内で単一クエリ結果のデータを設定する。
- 関数: wp_reset_postdata() - 以前のクエリを復元する(通常、ループ内のもう一つのループで使われる)。
- 関数: wp_reset_query()
- 関数: is_main_query() - 変更するのがメインクエリであるよう保証する。
- アクションフック: pre_get_posts - 実行される前に WordPress クエリを変更する。
- アクションフック: the_post - 投稿オブジェクトをクエリの後に変更する。
- フィルターフック: found_posts - found_posts WP_Query オブジェクトの値を変更する
関連
固定ページ: get_all_page_ids(), get_ancestors(), get_page_link(), get_page_by_path(), get_page_by_title(), get_page_children(), get_page_hierarchy(), get_page_uri() /en, get_pages(), is_page(), page_uri_index() /en, wp_list_pages(), wp_page_menu()
リスト・ドロップダウン関数: wp_list_authors(), wp_list_categories(), wp_list_pages(), wp_list_bookmarks(), wp_list_comments(), wp_get_archives(), wp_page_menu(), wp_dropdown_pages(), wp_dropdown_categories(), wp_dropdown_users()
最新英語版: WordPress Codex » Template Tags/wp_list_pages (最新版との差分)