Description【描述】
get_children() retrieves attachments, revisions, or sub-Pages, possibly by post parent. It works similarly to get_posts().【get_children() 可以根據(jù)父級(jí)文章檢索附件、修訂版本 或者 子頁(yè)面。它的作用和 get_posts() 相似。】
Synopsis【簡(jiǎn)介】
array $children =& get_children( mixed $args = "", constant $output = OBJECT);
Return values【返回值】
Returns an associative array of posts (of variable type set by $outputparameter) with post IDs as array keys, or an empty array if no posts are found.【返回一組以文章ID作為數(shù)組值的關(guān)聯(lián)文章(根據(jù) $outputparameter 的變量類(lèi)型設(shè)置);如果不存在文章,將返回一個(gè)空數(shù)組】
Prior to Version 2.9, the return value would be false when no children found.【在版本 2.9 之前,如果不存在子對(duì)象,返回的值將是錯(cuò)誤的】
Default parameters (Version 2.7)【默認(rèn)參數(shù)(版本 2.7)】
$defaults = array(
'post_parent' => 0,
'post_type' => 'any',
'numberposts' => -1,
'post_status' => 'any'
);
Parameters【參數(shù)】
See get_posts() for a full list of parameters.【要查看所有的參數(shù),請(qǐng)閱讀 get_posts() 】
- $args
- (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. 【(mixed)通過(guò) 一個(gè)查詢(xún)類(lèi)型的字符串或數(shù)組 設(shè)置(下面)幾個(gè)參數(shù)。通過(guò)一個(gè) 整數(shù) 的文章 ID 或 文章對(duì)象 將檢索到那篇文章的子對(duì)象;過(guò)一個(gè) 空值 將檢索到 當(dāng)前文章或頁(yè)面 的子對(duì)象。】
- $args[‘numberposts’]
- (integer) Number of child posts to retrieve. Optional; default: -1 (unlimited) 【(整數(shù))要檢索的子文章的數(shù)量。可選,默認(rèn):-1(不限制)】
- $args[‘post_parent’]
- (integer) Pass the ID of a post or Page to get its children. Pass 0 to get attachments without parent. Passnull to get any child regardless of parent. Optional; default: 0 (no parent) 【(整數(shù))通過(guò)文章或頁(yè)面的 ID 來(lái)檢索它的子對(duì)象。通過(guò) 0 獲取不包含父級(jí)的附件。通過(guò) null 獲取任何父級(jí)的子對(duì)象。可選,默認(rèn):0 (不包含父級(jí))】
- $args[‘post_type’]
- (string) Any value from post_type column of the posts table, such as attachment, page, or revision; or the keyword any. Default: any 【(字符串)來(lái)自文章表的 post_type(文章類(lèi)型) 那一欄的任何值,比如 附件、頁(yè)面、修訂版本 或 任何關(guān)鍵字。默認(rèn):any】
- $args[‘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 【(字符串)來(lái)自 wp_posts 表的 post_status(文章?tīng)顟B(tài)) 那一欄的任何值,比如 已發(fā)布、草稿、繼承 或任何關(guān)鍵字。默認(rèn):any】
- $args[‘post_mime_type’]
- (string) A full or partial mime-type, e.g. image, video, video/mp4, which is matched against a post’s post_mime_type field 【(字符串)一個(gè)完整的或部分的 mime-type,比如 圖像、視頻、視頻/MP4,它和一篇文章的 post_mime_type 字段相匹配 】
- $output
- (constant) Variable type of the array items returned by the function: one of OBJECT, ARRAY_A, ARRAY_N. Optional; default: OBJECT【(常量)由該函數(shù)返回的數(shù)組項(xiàng)的變量類(lèi)型:OBJECT, ARRAY_A, ARRAY_N 的其中一種。可選,默認(rèn):OBJECT】
Examples【例子】
If you just want to get or display attachments, it’s probably a little easier to use get_posts() instead.【如果你想要獲取或顯示附件,可能使用 get_posts() 會(huì)更加容易。】
$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【這里沒(méi)有附件】
} 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:【如果你沒(méi)必要處理一個(gè)空的結(jié)果:】
foreach ( (array) $videos as $attachment_id => $attachment ) {
echo wp_get_attachment_link( $attachment_id );
}
Show the first image associated with the post【展示與文章相關(guān)的第一張圖片】
This function retrieves the first image associated with a post【下面的函數(shù)將檢索與一篇文章相關(guān)的第一張圖片】
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">';
}
}
}
Show the first image associated with the post and re-key the array【展示與文章 和 重新輸入的數(shù)組 相關(guān)的第一張圖片】
In the example above, a primary array is keyed with the image ID (the exact thing which is being sought – since we don’t know it how are we supposed to access it?). The code below provides an easier handle for the image information: the array $child_image. Should be used in the loop.【在上面的例子中,一個(gè)最初的數(shù)組輸入的是圖片的 ID(正是要尋求的東西——因?yàn)槲覀儾恢牢覀儜?yīng)該如何訪問(wèn)它)。下面的代碼將提供一種更容易的處理圖片信息的方式:數(shù)組 $child_image。它應(yīng)該在循環(huán)中使用。】
$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); //輸出 $child_image 數(shù)組的內(nèi)容
echo $child_image['ID']; //輸出 $child_image 的ID
Change Log【更新日志】
Since: 2.0.0
Source File【源文件】
get_children() 在 wp-includes/post.php 文件中。
Related【相關(guān)函數(shù)】
get_children() calls get_posts(), which calls $WP_Query->get_posts().
wp_get_attachment_link(), get_page_children()
參考資料:http://codex.wordpress.org/Function_Reference/get_children





倡老師!!我是個(gè)初學(xué)者,然后用這里的代碼寫(xiě)了一個(gè)頁(yè)面用做輸出媒體庫(kù)中的圖片附件。
$images = get_children(‘post_type=attachment’);
//$images = get_posts(‘post_type=attachment’);
if ( empty($images) ) {
// no attachments here【這里沒(méi)有附件】
} else {
foreach ( $images as $attachment_id => $attachment ) {
/*echo $attachment_id.””;*/
echo wp_get_attachment_image( $attachment_id, ‘thumbnail’ );
}
}
就是這段代碼,上面說(shuō)get_children 和get_post類(lèi)似么,但是只有g(shù)et_children查詢(xún)時(shí)能輸出圖片,我打印遍歷后的attachment_id,只有g(shù)et_children時(shí)能打印,是他們的返回結(jié)果不一樣么?
倡萌講一下小工具掛鉤吧