Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



get_post_ancestors ›

Seit2.5.0
Veraltetn/v
get_post_ancestors ( $post )
Parameter:
  • (int|WP_Post) $post Post ID or post object.
    Erforderlich: Ja
Gibt zurück:
  • (array) Ancestor IDs or empty array if none are found.
Definiert in:
Codex:

Retrieve ancestors of a post.



Quellcode

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 = $ancestors[] = $post->post_parent;

	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 ) )
			break;

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

	return $ancestors;
}