WordPress 技巧:找到最顶层父页面的 ID
如果你的页面结构有多层级,下面的代码可以找到某个页面最顶层的父页面 ID:
<?php
if ($post->post_parent)	{
	$ancestors	= get_post_ancestors($post->ID);
	$root		= count($ancestors)-1;
	$parent		= $ancestors[$root];
} else {
	$parent		= $post->ID;
}
?>如果你的页面结构有多层级,下面的代码可以找到某个页面最顶层的父页面 ID:
<?php
if ($post->post_parent)	{
	$ancestors	= get_post_ancestors($post->ID);
	$root		= count($ancestors)-1;
	$parent		= $ancestors[$root];
} else {
	$parent		= $post->ID;
}
?>








