结果发现有一个wp_nav_menu_objects
过滤器,允许您在将导航菜单项加入字符串之前修改它们的数组。我能够使用以下功能完成我需要的任务:
function wpse121517_add_shop_menu_links( $items, $args ) {
if ( \'Shop Menu\' !== $args->menu )
return $items;
// Where to redirect after logging in or out
$redirect = get_permalink( get_option( \'woocommerce_myaccount_page_id\' ) );
$new_links = array();
if ( is_user_logged_in() ) {
$label = \'Logout\';
$link = wp_logout_url( $redirect );
// Create a nav_menu_item object to hold our link
// for My Account, only if user is logged-in
$item = array(
\'title\' => \'Account\',
\'menu_item_parent\' => 0,
\'ID\' => \'my-account\',
\'db_id\' => \'\',
\'url\' => get_permalink( get_option( \'woocommerce_myaccount_page_id\' ) ),
\'classes\' => array( \'menu-item\' )
);
$new_links[] = (object) $item; // Add the new menu item to our array
unset( $item );
} else {
$label = \'Login\';
$link = wp_login_url( $redirect );
}
// Create a nav_menu_item object to hold our link
// for login/out
$item = array(
\'title\' => $label,
\'menu_item_parent\' => 0,
\'ID\' => \'loginout\',
\'db_id\' => \'\',
\'url\' => $link,
\'classes\' => array( \'menu-item\' )
);
$new_links[] = (object) $item; // Add the new menu item to our array
$index = count( $items ) - 2; // Insert before the last two items
// Insert the new links at the appropriate place.
array_splice( $items, $index, 0, $new_links );
return $items;
}
add_filter( \'wp_nav_menu_objects\', \'wpse121517_add_shop_menu_links\', 10, 2 );