经过一些测试,我发现最好的方法是使用助行器。
对于本例,我们假设这是调用菜单的代码
wp_nav_menu([
\'container\' => false,
\'theme_location\' => \'right-side-top-menu\',
\'walker\' => new Menu_Middle_Logo()
]);
现在对于我们的walker来说,没有一种简单的方法来确定哪个父菜单项是中间的菜单项,但这段代码应该能够处理您的每个顶级菜单项场景。
class Menu_Middle_Logo extends Walker_Nav_Menu {
function end_el (&$output, $item, $depth = 0, $args = [], $id = 0) {
parent::end_el($output, $item, $depth, $args, $id);
// the menu slug
$theme_location = \'right-side-top-menu\';
// check for top level depth and if correct menu
if ($depth === 0 && isset($args->menu) && isset($args->menu->slug) && $args->menu->slug === $theme_location) {
// get current menu items
$menu_items = wp_get_nav_menu_items($theme_location);
// will indicate how many top level menu items we have
$parent_menu_items = [];
// get top level menu items
foreach ($menu_items as $menu_item) {
if ($menu_item->menu_item_parent == 0) $parent_menu_items[] = $menu_item;
}
// get total menu items halved
$half_menu_items = floor(count($parent_menu_items) / 2);
// the menu item we want to add the logo to
// before or after, depends if menu items are odd or even
if (is_float($half_menu_items)) {
// if you have odd menu items lets say three this will add after the first
// [1] [custom html] [2] [3]
// if you want it to add after the second remove the - 1, this will result in
// [1] [2] [custom html] [3]
$middle_item = $parent_menu_items[$half_menu_items - 1];
} elseif (is_int($half_menu_items)) {
// this handles even menu items
$middle_item = $parent_menu_items[$half_menu_items - 1];
}
// if current menu item is middle item, add our custom html
if (isset($middle_item) && $middle_item->ID === $item->ID) {
$output .= \'<li class="my-logo">\';
ob_start();
the_custom_logo();
// this is for testing because the_custom_logo()
// is not available in my theme
// echo 1234;
$output .= ob_get_clean();
$output .= \'</li>\';
}
}
}
}
将此walker代码复制到
functions.php
它应该会起作用
别忘了在中添加walker属性
wp_nav_menu
args。