當(dāng)前位置:首頁(yè)>WordPress建站>WordPress開(kāi)發(fā)>WordPress函數(shù):get_children (獲取子對(duì)象)

WordPress函數(shù):get_children (獲取子對(duì)象)

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

聲明:本站所有文章,如無(wú)特殊說(shuō)明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個(gè)人或組織,在未征得本站同意時(shí),禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書(shū)籍等各類(lèi)媒體平臺(tái)。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學(xué)公眾號(hào) WPDAXUE
WordPress開(kāi)發(fā)

WordPress函數(shù):get_boundary_post(獲取邊界文章)

2013-3-21 6:54:00

WordPress開(kāi)發(fā)

WordPress 自定義文章類(lèi)型 介紹及實(shí)例解說(shuō)(上)

2013-3-23 6:17:00

2 條回復(fù) A文章作者 M管理員
  1. cqhy

    倡老師!!我是個(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é)果不一樣么?

  2. 倡萌講一下小工具掛鉤吧

?
個(gè)人中心
購(gòu)物車(chē)
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

安西县| 广宗县| 土默特右旗| 汶川县| 鹤壁市| 南木林县| 锦屏县| 绍兴县| 新泰市| 哈密市| 苍溪县| 金塔县| 石林| 甘泉县| 临朐县| 巨鹿县| 永寿县| 洛南县| 巴里| 大竹县| 逊克县| 乌恰县| 洛宁县| 大厂| 昌黎县| 太康县| 台东县| 沈丘县| 华阴市| 敦煌市| 子洲县| 普安县| 青田县| 长丰县| 澄迈县| 迁安市| 双桥区| 故城县| 西乡县| 上虞市| 简阳市|