get_pages

Sponsored Link

この関数はブログで定義されている全てのページのリストを取得するために使用します。基本的にget_pagesはページの配列を与えて、配列はツリー状ではありません。ツリー状のような方法でページタイトルの出力のために、wp_list_pages()テンプレートを参照してください。

get_postsと同様にパラメータ名と異なる値であることに注意してください。

Sponsored Link

This function is used to get a list of all the pages that are defined in the blog. Essentially get_pages gives you an array of the pages, and that array is not tree-like. See the template tag, wp_list_pages(), for the output of page titles in a tree-like fashion.

Note that, although similar to get_posts, several of the parameter names and values differ.

get_pagesのファンクションタグ使用方法

<?php get_pages( $args ); ?>

概要

<?php $args = array(
    'child_of' => 0,
    'sort_order' => 'ASC',
    'sort_column' => 'post_title',
    'hierarchical' => 1,
    'exclude' => ,
    'include' => ,
    'meta_key' => ,
    'meta_value' => ,
    'authors' => ,
    'parent' => -1,
    'exclude_tree' => ,
    'number' => ,
    'offset' => 0,
    'post_type' => 'page',
    'post_status' => 'publish'
); ?>

get_pagesのパラメーター

sort_column

(string) 様々な方法のナンバーにページのリストをソートします。デフォルトの設定はページタイトルによってアルファベット順です。

(string) Sorts the list of Pages in a number of different ways. The default setting is sort alphabetically by Page title.

  • ‘post_title’ – (タイトルによって)アルファベットにページをソートします。 – デフォルト
  • ‘menu_order’ – ページの順序によってページをソートします。ページ順序とページIDとの違いに注意してください。ページIDは全ての投稿やページにWordPressによって割り当てられたユニーク番号です。ページ順序はWrite>Pagesの管理者パネルでユーザーによって設定することができます。
  • ‘post_date’ – 作成時間によってソートします。
  • ‘post_modified’ – 最後に更新した時間によってソートします。
  • ‘ID’ – 数値のページIDによってソートします。
  • ‘post_author’ – ページ著者の数値IDによってソートします。
  • ‘post_name’ – 投稿スラッグによってアルファベット順にソートします。WP3.2.1のように、まだサポートされていません (参照: http://core.trac.wordpress.org/ticket/14368 )
  • ‘post_title’ – Sort Pages alphabetically (by title) – default
  • ‘menu_order’ – Sort Pages by Page Order. N.B. Note the difference between Page Order and Page ID. The Page ID is a unique number assigned by WordPress to every post or page. The Page Order can be set by the user in the Write>Pages administrative panel.
  • ‘post_date’ – Sort by creation time.
  • ‘post_modified’ – Sort by time last modified.
  • ‘ID’ – Sort by numeric Page ID.
  • ‘post_author’ – Sort by the Page author’s numeric ID.
  • ‘post_name’ – Sort alphabetically by Post slug. Not supported yet, as of WP 3.2.1 (See: http://core.trac.wordpress.org/ticket/14368 )

注:sort_columnパラメーターはWordPressデーターベースのwp_postテーブル内に任意のフィールドの記述子によってページのリストをソートするために使用することができます。いくつかの有効な例はここにリストされています。

Note: The sort_column parameter can be used to sort the list of Pages by the descriptor of any field in the wp_post table of the WordPress database. Some useful examples are listed here.

注:get_posts()は、sort_column’の代わりに’orderby’パラメーターを使用します。また、get_posts()は、自動的にそれらの値に’post_’を付与します: ‘author, date, modified, parent, title, excerpt, content’.

Note: get_posts() uses the parameter ‘orderby’ instead of ‘sort_column’. Also, get_posts() automatically prepends ‘post_’ to these values: ‘author, date, modified, parent, title, excerpt, content’.

sort_order

(string) ページ(昇順または降順)のリストのソート順を変更します。デフォルトは昇順です。有効な値:

(string) Change the sort order of the list of Pages (either ascending or descending). The default is ascending. Valid values:

  • ‘asc’ – 最下位から最上位(デフォルト)にソートします。
  • ‘desc’ – 最上位から最下位に並べ替えます。
  • ‘asc’ – Sort from lowest to highest (Default).
  • ‘desc’ – Sort from highest to lowest.

注:get_posts()は、’sort_order’の代わりに’order’パラメーターを使用します。

Note: get_posts() uses the parameter ‘order’ instead of ‘sort_order’.

exclude

(string or array)リスト(例: ‘exclude=3,7,31’)から除外するページIDのカンマ区切りリストを定義します。バージョン3.0以降では、ページIDの配列を使用することも出来ます。デフォルト値はありません。

(string or array) Define a comma-separated list of Page IDs to be excluded from the list (example: ‘exclude=3,7,31’). Beginning with Version 3.0, an array of Page ID also can be used. There is no default value.

include

(string or array) get_pagesによって生成されたリスト内に特定のページのみ含みます。除外と同じように、このパラメーターはページIDのカンマ区切りのリストを指定します。バージョン3.0以降では、ページIDの配列を使用することも出来ます。デフォルト値はありません。

(string or array) Only include certain Pages in the list generated by get_pages. Like exclude, this parameter takes a comma-separated list of Page IDs. Beginning with Version 3.0, an array of Page ID also can be used. There is no default value.

child_of

(integer) シングルページのみのサブページを表示します;値としてページIDを使用します。デフォルトは0(全てのページを表示)。child_ofパラメーターは直接の子孫だけでなく、指定されたIDの”grandchildren”も取得することに注意してください。0 – デフォルト、child_of の制限はありません。

(integer) Displays the sub-pages of a single Page only; uses the ID for a Page as the value. Defaults to 0 (displays all Pages). Note that the child_of parameter will also fetch “grandchildren” of the given ID, not just direct descendants.
0 – default, no child_of restriction

parent

(integer)親としてこのIDをもつページを表示します。デフォルトは−1に設定されます(親関係なく全てのページを表示)。これは、子孫の1世代のみが取得される可能性があるので、child_ofパラメーターの’depth’を制限するために使用することができることに注意してください。child_ofパラメーターを結合に使用する必要があります。同じIDを供給します。また、階層的なパラメーターが0 (false) に設定する必要があることに注意してください。これはデフォルトではありません。 – さもないと結果がない場合、ルート(ID=0)以外のページに任意のページに返されません。

(integer) Displays those pages that have this ID as a parent. Defaults to -1 (displays all Pages regardless of parent). Note that this can be used to limit the ‘depth’ of the child_of parameter, so only one generation of descendants might be retrieved. You must use this in conjuction with the child_of parameter. Feed it the same ID. Also note that the hierarchical parameter must be set to 0 (false) — which is not default — or else no results will be returned for any page other than the root (ID=0).

  • -1 – デフォルトでは、親の制限がありません。
  • 0 – 全てのトップレベルのページを返します。
  • -1 – default, no parent restriction
  • 0 – returns all top level pages

exclude_tree

(integer) ‘child_of’, ‘exclude_tree’の反対は、結果から指定されたIDの全ての子を削除します。特定のページの全ての子を隠したりするのに役立ちます。’child_of’の値と組み合わせて孫を隠すために使用することができます。このパラメーターはバージョン2.7で利用可能でした。

(integer) The opposite of ‘child_of’, ‘exclude_tree’ will remove all children of a given ID from the results. Useful for hiding all children of a given page. Can also be used to hide grandchildren in conjunction with a ‘child_of’ value. This parameter was available at Version 2.7.

hierarchical

(boolean) ページのインラインをリスト表示あるいはそれらの親の下にインデントされた方法でサブページを表示します。デフォルトはtrueです(親リストの項目の下にインデントされたサブページを表示します)。有効な値:

(boolean) Display sub-Pages in an indented manner below their parent or list the Pages inline. The default is true (display sub-Pages indented below the parent list item). Valid values:

  • 1 (true) – default
  • 0 (false)

meta_key

(string)このカスタムフィールドキー(meta_valueフィールドと組み合わせて使用)を持つページのみ含みます。

(string) Only include the Pages that have this Custom Field Key (use in conjunction with the meta_value field).

meta_value

(string)このカスタムフィールド値(meta_keyフィールドと組み合わせて使用)を持つページのみ含みます。

(string) Only include the Pages that have this Custom Field Value (use in conjunction with the meta_key field).

authors

(string) 任意の著者によって書かれたページのみ含みます。
注:get_posts() は、’authors’の代わりに’author’を使用します。

(string) Only include the Pages written by the given author(s)
Note: get_posts() uses the parameter ‘author’ instead of ‘authors’.

number

(integer) 表示するページ数を設定します。このケースは、SQL LIMITの値が定義されています。LIMITがないのはデフォルトです。このパラメーターはバージョン2.8で追加されました。注:get_posts()は’number’の代わりに’numberposts’を使用します。

(integer) 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.
Note: get_posts() uses the parameter ‘numberposts’ instead of ‘number’.

offset

(integer) ページの設定を収集する前に引き渡す(あるいは置換する)ためのページナンバーです。デフォルトはOFFSETなしです。このパラメーターは、バージョン2.8で追加されました。

(integer) 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.

戻り値

(Array)
要求に一致する全てのページを含む配列です。

(Array)
An array containing all the Pages matching the request

ドロップダウンリストでページを表示する方法

この例は、全てのページをドロップダウンリスト表示します。ページのIDを渡す関数get_page_linkを単純に呼び出すとページへのリンクを取り込む事ができる方法に注意してください。

In this example a dropdown list with all the pages. Note how you can grab the link for the page with a simple call to the function get_page_link passing the ID of the page.

<select name="page-dropdown"
 onchange='document.location.href=this.options&#91;this.selectedIndex&#93;.value;'> 
<option value="">
<?php echo esc_attr( __( 'Select page' ) ); ?></option> 
<?php 
  $pages = get_pages(); 
  foreach ( $pages as $pagg ) {
  	$option = '<option value="' . get_page_link( $pagg->ID ) . '">';
	$option .= $pagg->post_title;
	$option .= '</option>';
	echo $option;
  }
 ?>
</select>

投稿フォーマットに現在のページの子ページを表示する方法

<?php
	$mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );

	foreach( $mypages as $page ) {		
		$content = $page->post_content;
		if ( ! $content ) // Check for empty page
			continue;

		$content = apply_filters( 'the_content', $content );
	?>
		<h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
		<div class="entry"><?php echo $content; ?></div>
	<?php
	}	
?>

変更ログ

Since: 1.5.0

ソースファイル

get_pages()は、wp-includes/post.php内に位置しています。

get_pages() is located in wp-includes/post.php.

関連ファンクションタグ

Page Tags: get_all_page_ids(), get_ancestors(), get_page(), get_page_link(), get_page_by_path(), get_page_by_title(), get_page_children(), get_page_hierarchy(), get_page_uri(), get_pages(), is_page(), page_uri_index(), wp_list_pages(), wp_page_menu()

Sponsored Link