不久前,当我遇到类似问题时,我在网上找到了这个过滤器。我想显示在wordpress后端的外观->菜单中设置的所有子页面。我只想显示当前页面的子级,而不使用get\\u children。I would give credit to whoever wrote this beauty, but I can\'t find the source.
此过滤器为功能wp\\U nav\\U菜单添加了一个额外选项。它允许您添加参数submenu. 唯一的缺陷是您需要使用父标题(而不是ID)进行选择,但您可以从页面标题中获得。
add_filter( \'wp_nav_menu_objects\', \'submenu_limit\', 10, 2 );
function submenu_limit( $items, $args ) {
if ( empty($args->submenu) )
return $items;
$parent_id = array_pop( wp_filter_object_list( $items, array( \'title\' => $args->submenu ), \'and\', \'ID\' ) );
$children = submenu_get_children_ids( $parent_id, $items );
foreach ( $items as $key => $item ) {
if ( ! in_array( $item->ID, $children ) )
unset($items[$key]);
}
return $items;
}
function submenu_get_children_ids( $id, $items ) {
$ids = wp_filter_object_list( $items, array( \'menu_item_parent\' => $id ), \'and\', \'ID\' );
foreach ( $ids as $id ) {
$ids = array_merge( $ids, submenu_get_children_ids( $id, $items ) );
}
return $ids;
}
这是我在侧边栏中使用它的方式:
<?php
//Get parent title
$menu_title = empty($post->post_parent) ? get_the_title( $post->ID ) : get_the_title( $post->post_parent );
wp_nav_menu(array(\'menu\' => \'main-menu\',\'submenu\' => $menu_title));
?>