get_children

函式
get_children ( $args = '', $output = OBJECT )
引數
  • (mixed) $args Optional. User defined arguments for replacing the defaults. Default empty.
    Required:
    Default: (empty)
  • (string) $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT.
    Required:
    Default: OBJECT
返回值
  • (WP_Post[]|array[]|int[]) Array of post objects, arrays, or IDs, depending on `$output`.
相關
  • get_posts()
定義位置
相關方法
get_term_childrenget_page_children_get_term_children_relocate_childrenget_category_children
引入
2.0.0
棄用
-

get_children: 這個函式用來檢索一個文章或頁面的孩子。它需要兩個引數:$post_id(必填)和$args(可選)。$post_id引數是你想檢索的文章或頁面的ID。引數$args是一個引數陣列,可用於修改查詢。

檢索文章父級ID的所有子代。

通常,在沒有任何增強的情況下,子句將適用於頁面。在WordPress的內部工作環境中,頁面、文章和附件共享同一個表,因此,這個功能可以適用於其中的任何一個。然後指出,雖然這個功能對文章不起作用,但並不意味著它對文章不起作用。建議你知道你希望檢索的是什麼上下文的子代。

附件也可以作為文章的子項,所以如果這是一個準確的說法(需要驗證),那麼就有可能獲得一個文章的所有附件。自2.5版本以來,附件已經發生了變化,所以這很可能是不準確的,但一般來說可以作為一個例子來說明什麼是可能的。

作為預設值列出的引數是這個函式的引數,也是get_posts()函式的引數。這些引數與get_children的預設值相結合,然後傳遞給get_posts()函式,該函式接受額外的引數。你可以替換這個函式中的預設值,列在下面,以及get_posts()函式中列出的額外引數。

post_parent”是最重要的引數,需要對$args引數給予重要關注。如果你傳遞一個物件或一個整數(數字),那麼只抓取’post_parent’,其他的都會丟失。如果你沒有指定任何引數,那麼就會認為你是在The Loop中,並且將從當前文章中抓取post parent。

post_parent”引數是用來獲取子代的ID。numberposts’是要檢索的文章數量,預設為’-1’,用於獲取所有的文章。給出一個高於0的數字將只檢索該數量的文章。

post_type’和’post_status’引數可以用來選擇要檢索的文章的標準。post_type”可以是任何東西,但WordPress的文章型別是”post”、”pages”和”attachments”。post_status’引數將接受編寫管理面板中的任何文章狀態。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_children( $args = '', $output = OBJECT ) {
$kids = array();
if ( empty( $args ) ) {
if ( isset( $GLOBALS['post'] ) ) {
$args = array( 'post_parent' => (int) $GLOBALS['post']->post_parent );
} else {
return $kids;
}
} elseif ( is_object( $args ) ) {
$args = array( 'post_parent' => (int) $args->post_parent );
} elseif ( is_numeric( $args ) ) {
$args = array( 'post_parent' => (int) $args );
}
$defaults = array(
'numberposts' => -1,
'post_type' => 'any',
'post_status' => 'any',
'post_parent' => 0,
);
$parsed_args = wp_parse_args( $args, $defaults );
$children = get_posts( $parsed_args );
if ( ! $children ) {
return $kids;
}
if ( ! empty( $parsed_args['fields'] ) ) {
return $children;
}
update_post_cache( $children );
foreach ( $children as $key => $child ) {
$kids[ $child->ID ] = $children[ $key ];
}
if ( OBJECT === $output ) {
return $kids;
} elseif ( ARRAY_A === $output ) {
$weeuns = array();
foreach ( (array) $kids as $kid ) {
$weeuns[ $kid->ID ] = get_object_vars( $kids[ $kid->ID ] );
}
return $weeuns;
} elseif ( ARRAY_N === $output ) {
$babes = array();
foreach ( (array) $kids as $kid ) {
$babes[ $kid->ID ] = array_values( get_object_vars( $kids[ $kid->ID ] ) );
}
return $babes;
} else {
return $kids;
}
}
function get_children( $args = '', $output = OBJECT ) { $kids = array(); if ( empty( $args ) ) { if ( isset( $GLOBALS['post'] ) ) { $args = array( 'post_parent' => (int) $GLOBALS['post']->post_parent ); } else { return $kids; } } elseif ( is_object( $args ) ) { $args = array( 'post_parent' => (int) $args->post_parent ); } elseif ( is_numeric( $args ) ) { $args = array( 'post_parent' => (int) $args ); } $defaults = array( 'numberposts' => -1, 'post_type' => 'any', 'post_status' => 'any', 'post_parent' => 0, ); $parsed_args = wp_parse_args( $args, $defaults ); $children = get_posts( $parsed_args ); if ( ! $children ) { return $kids; } if ( ! empty( $parsed_args['fields'] ) ) { return $children; } update_post_cache( $children ); foreach ( $children as $key => $child ) { $kids[ $child->ID ] = $children[ $key ]; } if ( OBJECT === $output ) { return $kids; } elseif ( ARRAY_A === $output ) { $weeuns = array(); foreach ( (array) $kids as $kid ) { $weeuns[ $kid->ID ] = get_object_vars( $kids[ $kid->ID ] ); } return $weeuns; } elseif ( ARRAY_N === $output ) { $babes = array(); foreach ( (array) $kids as $kid ) { $babes[ $kid->ID ] = array_values( get_object_vars( $kids[ $kid->ID ] ) ); } return $babes; } else { return $kids; } }
function get_children( $args = '', $output = OBJECT ) {
	$kids = array();
	if ( empty( $args ) ) {
		if ( isset( $GLOBALS['post'] ) ) {
			$args = array( 'post_parent' => (int) $GLOBALS['post']->post_parent );
		} else {
			return $kids;
		}
	} elseif ( is_object( $args ) ) {
		$args = array( 'post_parent' => (int) $args->post_parent );
	} elseif ( is_numeric( $args ) ) {
		$args = array( 'post_parent' => (int) $args );
	}

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

	$parsed_args = wp_parse_args( $args, $defaults );

	$children = get_posts( $parsed_args );

	if ( ! $children ) {
		return $kids;
	}

	if ( ! empty( $parsed_args['fields'] ) ) {
		return $children;
	}

	update_post_cache( $children );

	foreach ( $children as $key => $child ) {
		$kids[ $child->ID ] = $children[ $key ];
	}

	if ( OBJECT === $output ) {
		return $kids;
	} elseif ( ARRAY_A === $output ) {
		$weeuns = array();
		foreach ( (array) $kids as $kid ) {
			$weeuns[ $kid->ID ] = get_object_vars( $kids[ $kid->ID ] );
		}
		return $weeuns;
	} elseif ( ARRAY_N === $output ) {
		$babes = array();
		foreach ( (array) $kids as $kid ) {
			$babes[ $kid->ID ] = array_values( get_object_vars( $kids[ $kid->ID ] ) );
		}
		return $babes;
	} else {
		return $kids;
	}
}

常見問題

FAQs
檢視更多 >