这将在嵌套的子页面中找到最顶层父页面的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
翻译自: https://css-tricks.com/snippets/wordpress/find-id-of-top-most-parent-page/