get_post_ancestors

函式
get_post_ancestors ( $post )
引數
  • (int|WP_Post) $post Post ID or post object.
    Required:
返回值
  • (int[]) Array of ancestor IDs or empty array if there are none.
定義位置
相關方法
_get_post_ancestorsget_ancestorsget_post_customget_post_classget_post_custom_keys
引入
2.5.0
棄用
-

get_post_ancesters函式用於檢索給定文章的所有祖先文章的陣列。它只接受一個引數,即文章的ID: 此函式返回的陣列包括父文章及其所有父文章。

檢索一個文章的祖先的ID。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_post_ancestors( $post ) {
$post = get_post( $post );
if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID ) {
return array();
}
$ancestors = array();
$id = $post->post_parent;
$ancestors[] = $id;
while ( $ancestor = get_post( $id ) ) {
// Loop detection: If the ancestor has been seen before, break.
if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors, true ) ) {
break;
}
$id = $ancestor->post_parent;
$ancestors[] = $id;
}
return $ancestors;
}
function get_post_ancestors( $post ) { $post = get_post( $post ); if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID ) { return array(); } $ancestors = array(); $id = $post->post_parent; $ancestors[] = $id; while ( $ancestor = get_post( $id ) ) { // Loop detection: If the ancestor has been seen before, break. if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors, true ) ) { break; } $id = $ancestor->post_parent; $ancestors[] = $id; } return $ancestors; }
function get_post_ancestors( $post ) {
	$post = get_post( $post );

	if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID ) {
		return array();
	}

	$ancestors = array();

	$id          = $post->post_parent;
	$ancestors[] = $id;

	while ( $ancestor = get_post( $id ) ) {
		// Loop detection: If the ancestor has been seen before, break.
		if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors, true ) ) {
			break;
		}

		$id          = $ancestor->post_parent;
		$ancestors[] = $id;
	}

	return $ancestors;
}

常見問題

FAQs
檢視更多 >