万が一、当サイトで重大な問題を発見した際などは、フォーラムや WordSlack #docs チャンネルでお知らせください。</p>
関数リファレンス/get children
目次
説明
get_children() は指定した投稿の添付ファイルページやサブページやリビジョンを取得します。投稿の一覧を取得する機能は get_posts() とほぼ同じです。
使い方
<?php get_children( $args, $output ); ?>
デフォルトの使い方
<?php $args = array( 'post_parent' => 0, 'post_type' => 'any', 'numberposts' => -1, 'post_status' => 'any' ); $children_array = get_children( $args, 'OBJECT' ); ?> ?>
パラメータ
- $args
- (配列) (オプション) 取得対象を指定します。
- 初期値: なし
- $output
- (文字列) (オプション) 戻り値の型
- 初期値: 'OBJECT'
-
'ARRAY_A'
- 連想配列 -
'ARRAY_N'
- インデックス配列 -
'OBJECT'
- 投稿オブジェクト
引数
バージョン 2.6 以降、post_type パラメータには空でない文字列を指定しなければなりません(attachment または page)。
クエリ形式の文字列または配列 に以下のパラメータを指定します。post_parent に投稿 ID を指定すると、その投稿を親に持つ投稿を取得します。ID などの指定が無い場合は、親を持たない投稿を取得します。
- 'numberposts'
- (整数) (オプション) 取得する投稿の数
- 初期値: -1
-
'-1'
- すべての投稿
- 'post_parent'
- (整数) (オプション) 親投稿の ID
- 初期値: 0
-
'0'
- 親を持たない投稿
- 'post_type'
- (文字列) (オプション) 投稿タイプ
- 初期値: 'any'
-
'attachment'
- 添付ファイル -
'page'
- 固定ページ -
'revision'
- 投稿リビジョン -
'any'
- すべての投稿タイプ
- 'post_status'
- (文字列) (オプション) 投稿ステータス
- 初期値: 'any'
-
'publish'
- 公開 -
'draft'
- 下書き -
'inherit'
- 継承 -
'any'
- すべての投稿ステータス
- 'post_mime_type'
- (文字列) (オプション) MIMEタイプ
- 初期値: なし
-
'image'
- 画像 -
'video'
- 動画 -
'videp/mp4'
- 動画(mp4)
参考: $args パラメーターの完全なリストは get_posts() を見てください。
戻り値
- 連想配列/インデックス配列/投稿オブジェクトの配列
- $output で指定した型で投稿の一覧を返します。投稿 ID が連想配列のキーになります。
参考: 投稿が無かった場合、バージョン 2.9 以降は空の配列、それ以前は false を返します。
用例
添付ファイルを表示する場合、get_posts()
を使うより簡単です:
$images =& get_children( 'post_type=attachment&post_mime_type=image' ); $videos =& get_children( 'post_type=attachment&post_mime_type=video/mp4' ); if ( empty($images) ) { // 添付ファイルが無い場合 } else { foreach ( $images as $attachment_id => $attachment ) { echo wp_get_attachment_image( $attachment_id, 'full' ); } } // 添付ファイルが無い場合のコードを書かない方法: foreach ( (array) $videos as $attachment_id => $attachment ) { echo wp_get_attachment_link( $attachment_id ); }
投稿に添付された最初の画像を表示
この関数は投稿に添付された最初の画像を表示します。
<?php function echo_first_image( $postID ) { $args = array( 'numberposts' => 1, 'order' => 'ASC', 'post_mime_type' => 'image', 'post_parent' => $postID, 'post_status' => null, 'post_type' => 'attachment', ); $attachments = get_children( $args ); if ( $attachments ) { foreach ( $attachments as $attachment ) { $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) ? wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment->ID, 'full' ); echo '<img src="' . wp_get_attachment_thumb_url( $attachment->ID ) . '" class="current">'; } } }
投稿に添付された最初の画像を表示して配列のキーを振りなおす
上の例では配列のキーが画像の ID になります(でもどのようにしてアクセスすればよいのでしょう?)。次のコードは画像の情報を $child_image という配列へ入れてアクセスしやすくします。これはループ内で使ってください。
$args = array( 'numberposts' => 1, 'order'=> 'DESC', 'post_mime_type' => 'image', 'post_parent' => $post->ID, 'post_type' => 'attachment' ); $get_children_array = get_children($args,ARRAY_A); //returns Array ( [$image_ID]... $rekeyed_array = array_values($get_children_array); $child_image = $rekeyed_array[0]; print_r($child_image); //Show the contents of the $child_image array. echo $child_image['ID']; //Show the $child_image ID.
変更履歴
- 2.0 : 新規導入
ソースファイル
get_children() は wp-includes/post.php
にあります。
関連項目
get_children() は get_posts() を呼び出します。get_posts() は $WP_Query->get_posts() を呼び出します。
添付ファイル関数:
get_children(),
get attached media(),
the_attachment_link(),
get_attachment_link(),
wp_get_attachment_link(),
wp_get_attachment_image(),
wp_get_attachment_image_src(),
wp_get_attachment_url(),
wp_get_attachment_thumb_file(),
wp_get_attachment_thumb_url(),
is_attachment(),
wp_get_attachment_metadata()
記事
- 記事: ループ - 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 » Function Reference/get children (最新版との差分)