get_children

Sponsored Link

get_children()は、親記事によって可能な、添付ファイル、リビジョン、またはサブページを取得します。get_posts()と同様な動作をします。

Sponsored Link

get_children() retrieves attachments, revisions, or sub-Pages, possibly by post parent. It works similarly to get_posts().

概要

array|false $children =& get_children( mixed $args = "", constant $output = OBJECT);

戻り値

配列キーとして投稿IDを持つ投稿の連想配列($outputパラメータによって設定された変数の型の)、または投稿が見つからない場合は、空の配列を返します。

Returns an associative array of posts (of variable type set by $output parameter) with post IDs as array keys, or an empty array if no posts are found.

前のバージョン2.9は、戻り値が子が見つからなかったときにfalseとなります。

Prior to Version 2.9, the return value would be false when no children found.

添付ファイルを取得あるいは表示したい場合は、恐らく少し簡単にget_posts()の代わりに使用する事ができます。

If you just want to get or display attachments, it’s probably a little easier to use get_posts() instead.

$images =& get_children( 'post_type=attachment&post_mime_type=image' );

$videos =& get_children( 'post_type=attachment&post_mime_type=video/mp4' );

if ( empty($images) ) {
	// no attachments here
} else {
	foreach ( $images as $attachment_id => $attachment ) {
		echo wp_get_attachment_image( $attachment_id, 'full' );
	}
}

//  If you don't need to handle an empty result:

foreach ( (array) $videos as $attachment_id => $attachment ) {
	echo wp_get_attachment_link( $attachment_id );
}

記事に関連付けられている最初の画像を表示する方法

この関数は、記事に関連付けられた最初の画像を取得します。

This function retrieves the first image associated with a post

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 );
	
	//print_r($attachments);
	
	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">';
			
		}
	}
}

デフォルトのパラメーター (バージョン 2.7)

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

get childrenのパラメーター

パラメーターの完全なリストは、get_posts()を参照して下さい。

See get_posts() for a full list of parameters.

バージョン2.6のように、空でないpost_typeパラメーターを渡す必要があります(添付ファイルあるいはページのいづれか)。

As of Version 2.6, you must pass a non-empty post_type parameter (either attachment or page).

$args

クエリ形式の文字列や配列を渡す(混合する)事は、いくつかのパラメーター(下記参照)を設定します。整数の記事IDや記事のオブジェクトを渡す事は、記事の子を取得します。空の値を渡す事は、現在の記事あるいはページの子を取得します。

(mixed) Passing a query-style string or array sets several parameters (below). Passing an integer post ID or a post object will retrieve children of that post; passing an empty value will retrieve children of the current post or Page.

$args[‘numberposts’]

(integer) 子記事の数を取得します。オプション;デフォルト:-1 (無制限)

(integer) Number of child posts to retrieve. Optional; default: -1 (unlimited)

$args[‘post_parent’]

(integer) 子を取得するために、記事またはページのIDを渡します。親なしに添付ファイルを取得するために0を渡します。親に関係なく任意の子を取得するためにnullを渡します。オプション;デフォルト:0(親なし)

(integer) Pass the ID of a post or Page to get its children. Pass 0 to get attachments without parent. Pass null to get any child regardless of parent. Optional; default: 0 (no parent)

$args[‘post_type’]

(string) 記事テーブルのpost_typeカラムからのいづれかの値です。例えば、添付ファイル、ページあるいはリビジョン、任意のキーワード。デフォルト:any

(string) Any value from post_type column of the posts table, such as attachment, page, or revision; or the keyword any. Default: any

$args[‘post_status’]

(string) wp_posts tableのpost_statusカラムからいづれかの値です。例えば、公開、下書き、継承、または任意のキーワード。

(string) Any value from the post_status column of the wp_posts table, such as publish, draft, or inherit; or the keyword any. Default: any

$args[‘post_mime_type’]

(string) 全部または一部のmime-type、例えば、記事のpost_mime_typeフィールドと照合される、画像、動画、video/mp4です。

(string) A full or partial mime-type, e.g. image, video, video/mp4, which is matched against a post’s post_mime_type field

$output

(constant) 関数によって返される配列項目の変数の型:OBJECT、ARRAY_A, ARRAY_Nの1つ。オプション;デフォルト:OBJECT

(constant) Variable type of the array items returned by the function: one of OBJECT, ARRAY_A, ARRAY_N. Optional; default: OBJECT

関連ファンクションタグ

get_children()は、get_posts()を呼び出し、$WP_Query->get_posts()を呼び出します。

get_children() calls get_posts(), which calls $WP_Query->get_posts().

wp_get_attachment_link(), get_page_children()

Sponsored Link