我无法告诉您此更改是何时发生的,但从WordPress版本5.2.4开始,您可以使用object
和object_id
要检索页面ID,请执行以下操作:
/**
* The `0` is added as a default case.
* In case our menu does not contain any pages this
* will prevent our query from returning all pages
*/
$page_ids = array( 0 );
$registered_menu = \'my_registered_menu\';
$locations = get_nav_menu_locations();
if( ! empty( $locations ) && isset( $locations[ $registered_menu ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $registered_menu ] );
$menu_items = wp_get_nav_menu_items( $menu->term_id );
if( ! empty( $menu_items ) ) {
foreach( $menu_items as $item ) {
// Only grab page IDs
if( \'page\' !== $item->object ) {
continue;
}
$page_ids[] = $item->object_id;
}
}
}
$page_query = new WP_Query( array(
\'post_type\' => \'page\',
\'post_status\' => \'publish\',
\'no_found_rows\' => true,
\'post__in\' => $page_ids,
\'posts_per_page\'=> 100, // Large upper limit
) );