• サイトデザイン工事中です。ご意見をお寄せください
  • 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

関数リファレンス/get children

提供:WordPress Codex 日本語版
移動: 案内, 検索

get_children() は指定した投稿の添付ファイルページやサブページやリビジョンを取得します。投稿の一覧を取得する機能は get_posts() とほぼ同じです。

使い方

<?php $children = &get_children( $args, $output); ?>

パラメータ


get_posts() も見てください。

$args
配列) (オプション) パラメータ
初期値: なし

デフォルトは以下の通り

$defaults = array( 
    'post_parent' => 0,
    'post_type'   => 'any', 
    'numberposts' => -1,
    'post_status' => 'any',
);

クエリ形式の文字列または配列 に以下のパラメータを指定します。post_parent に投稿 ID を指定すると、その投稿を親に持つ投稿を取得します。ID などの指定が無い場合は、親を持たない投稿を取得します。

$args['numberposts']
整数) (オプション) 取得する投稿の数
初期値: -1
  • '-1' - すべての投稿
$args['post_parent']
整数) (オプション) 親投稿の ID
初期値: 0
  • '0' - 親を持たない投稿
$args['post_type']
文字列) (オプション) 投稿タイプ
初期値: 'any'
  • 'attachment' - 添付ファイル
  • 'page' - 固定ページ
  • 'revision' - 投稿リビジョン
  • 'any' - すべての投稿タイプ
$args['post_status']
文字列) (オプション) 投稿ステータス
初期値: 'any'
  • 'publish' - 公開
  • 'draft' - 下書き
  • 'inherit' - 継承
  • 'any' - すべての投稿ステータス
$args['post_mime_type']
文字列) (オプション) MIMEタイプ
初期値: なし
  • 'image' - 画像
  • 'video' - 動画
  • 'videp/mp4' - 動画(mp4)
$output
定数) (オプション) 戻り値の型
初期値: OBJECT
  • 'ARRAY_A' - 連想配列
  • 'ARRAY_N' - インデックス配列
  • 'OBJECT' - 投稿オブジェクト

戻り値

連想配列/インデックス配列/投稿オブジェクト 
$output で指定した型で投稿の一覧を返します。投稿が無かった場合、バージョン 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 );
}

変更履歴

ソースファイル

get_children()wp-includes/post.php に含まれています。

get_children()get_posts() を呼んでいます。get_posts()$WP_Query->get_posts() を呼んでいます。

wp_title, get_posts, query_posts, the_search_query


最新英語版: WordPress Codex » Function Reference/get children最新版との差分

個人用ツール
名前空間
変種
操作
このサイトについて
特集
サーバ別情報
リファレンス
ドキュメント整備
ツールボックス
他の言語