查找最顶层父页面的 ID

Avatar of Chris Coyier
Chris Coyier

这将找到嵌套子页面中最顶层父页面的 ID。例如,您正在查看的此页面嵌套在

<?php

if ($post->post_parent)	{
	$ancestors=get_post_ancestors($post->ID);
	$root=count($ancestors)-1;
	$parent = $ancestors[$root];
} else {
	$parent = $post->ID;
}

?>

$parent 将是正确的 ID。例如,与 wp_list_pages 一起使用。

来源:CSSGlobe