wpseek.com
WordPress开发者和主题制作者的搜索引擎



get_post_ancestors › WordPress Function

Since2.5.0
已弃用n/a
get_post_ancestors ( $post )
参数:
  • (int|WP_Post) $post Post ID or post object.
    Required: Yes
返回:
  • (int[]) Array of ancestor IDs or empty array if there are none.
定义在:
文档:

Retrieves the IDs of the ancestors of a post.



源码

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;
}