我有一个来自wordpress的自定义菜单>外观>使用wp查询从db菜单和绑定子菜单。我面临的问题是子菜单在Default Languange(英语)中具有约束力,但是当我用希伯来语翻译网站时,子菜单不具有约束力。我检查了代码,发现wp_get_nav_menu_items过滤器没有使用希伯来语。
这是我的代码:
$menu_name = 'Main Navigation';
$name_of_menu_item_to_append_to = 'Where can I use it';
$id_of_menu_item_to_append_to = get_wp_object_id( $name_of_menu_item_to_append_to, 'nav_menu_item' );
global $wpdb;
$get_continents = $wpdb->get_results
(
"SELECT * FROM ".$wpdb->prefix . "continents ORDER BY continent_id ASC"
);
if(count($get_continents) > 0)
{
foreach ($get_continents as $continent)
{
$new_submenu_item = array(
'text' => $continent->continent_name,
'url' => '#'
);
add_subitems_to_menu($menu_name,$id_of_menu_item_to_append_to,array( $new_submenu_item ),$continent->continent_id);
}
}
function add_subitems_to_menu( $menu_name, $parent_object_id, $subitems, $continent_id ) {
if ( is_admin() ) {
return;
}
// Use wp_get_nav_menu_items filter, is used by Timber to get WP menu items
add_filter( 'wp_get_nav_menu_items', function( $items, $menu )
use( $menu_name, $parent_object_id, $subitems, $continent_id ) {
// If no menu found, just return the items without adding anything
if ( $menu->name != $menu_name && $menu->slug != $menu_name ) {
return $items;
}
// Find the menu item ID corresponding to the given post/page object ID
// If no post/page found, the subitems won't have any parent (will be on 1st level)
$parent_menu_item_id = 0;
foreach ( $items as $item ) {
if ( $parent_object_id == $item->object_id ) {
$parent_menu_item_id = $item->ID;
break;
}
}
$menu_order = count( $items ) + 1;
foreach ($subitems as $subitem) {
// Create objects containing all (and only) those properties from WP_Post
// used by WP to create a menu item
$items[] = (object) array(
'ID' => $continent_id, // ID that WP won't use
'title' => $subitem['text'],
'url' => $subitem['url'],
'menu_item_parent' => $parent_menu_item_id,
'menu_order' => $menu_order,
'post_name' => '',
// These are not necessary, but PHP warning will be thrown if undefined
'type' => '',
'object' => '',
'object_id' => $continent_id,
'db_id' => $continent_id,
'classes' => '',
);
$menu_order++;
}
return $items;
}, 10, 2);
}
我希望你理解我想要解释的内容。