Bainternet的解决方案肯定会奏效,但如果您想使用页面slug而不是ID,可以将其放入函数中。php文件:
// GET PAGE ID FROM THE SLUG, HELPER FUNCTION FOR IS_TREE
function get_ID_by_slug($page_slug) {
$page = get_page_by_path($page_slug);
if ($page) {
return $page->ID;
} else {
return null;
}
}
// DETERMINE IF A PAGE IS IN A DESCENDANT TREE
function is_tree($pid) { // $pid = The ID of the page we\'re looking for pages underneath
global $post; // load details about this page
$pid = get_ID_by_slug($pid);
if( is_page() && ($post->post_parent==$pid || is_page($pid) ) )
return true; // we\'re at the page or at a sub page
else
return false; // we\'re elsewhere
};
然后将其用于侧栏中的条件语句。php或其他:
<?php if ( is_tree(\'advice\') ) {
wp_nav_menu( array( \'theme_location\' => \'advice-menu\' ) );
} elseif ( is_tree(\'another-page-slug\') ) {
wp_nav_menu( array( \'theme_location\' => \'another-page-menu\' ) );
} ?>
这将返回“树”中的任何页面,包括父页面本身及其下的所有子页面。希望这有帮助,祝你好运!